⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 patientexplorer.pas

📁 PatientRunner 20 Source
💻 PAS
📖 第 1 页 / 共 4 页
字号:
  //to avoid nasty access error.
  if not Assigned(NotesListView.ItemFocused.Data) then Exit;

  //The PreviousItem pointer marks the last note that was displayed.
  //If PreviousItem is nil as it is by default, no note has even been displayed.
  //The PreviousItem pointer will be reset to the current note.  This will
  //result in a screen update to the current note.

  //NOTE:  When the form is closed and the NotesListView is destroyed of if
  //items are deleted from the list, the PreviousItem pointer must be reset.

  //If a previous note existed, check to see if the newly selected note is
  //different then the previously selected note.  If this is the case, any
  //possible note changes need to be updated in the previously selected note's
  //record and Modded is set to true.  This way when the user tries to exit, a
  //quick scan can detect if any notes need to be saved so the user can be
  //prompted to save.

  //If a previous note existed and the newly selected note is the same as the
  //previously selected one, just exit the procedure because there is no need
  //to update the screen.

  if PreviousItem = nil then
    PreviousItem:=NotesListView.ItemFocused
  else
  begin
    if NotesListView.ItemFocused <> PreviousItem then
    begin
      if not Assigned(PreviousItem.Data) then Exit;

      if DescriptionEdit.Modified or NoteRichEdit.Modified
        or PNoteRecord(PreviousItem.Data)^.NewNote
        or PNoteRecord(PreviousItem.Data)^.Modded
        or (PNoteRecord(PreviousItem.Data)^.DictationPending<>DictationPendingCheckBox.Checked)
        or (PNoteRecord(PreviousItem.Data)^.PleaseReview<>PleaseReviewCheckBox.Checked) then
      begin
        with PNoteRecord(PreviousItem.Data)^ do
        begin
          Modded:=True;
          Description:=DescriptionEdit.Text;
          //PreviousItem.Caption:=DatetoStr(PNoteRecord(PreviousItem.Data)^.NoteDateTime);
          PreviousItem.SubItems[0]:=PNoteRecord(PreviousItem.Data)^.Description;
          DictationPending:=DictationPendingCheckBox.Checked;
          PleaseReview:=PleaseReviewCheckBox.Checked;
          NoteStringStream.Position:=0;
          NoteRichEdit.Lines.SaveToStream(NoteStringStream);
        end;
      end; //end if note modified
      PreviousItem:=NotesListView.ItemFocused;
    end
    else
      Exit; //Click note is same as previously clicked note
  end;

  //Now that any possible changes of the old note have been saved into its
  //record, update screen with newly selected note

  with PNoteRecord(NotesListView.ItemFocused.Data)^ do
  begin
    DescriptionEdit.Text:=Description;
    DictationPendingCheckBox.Checked:=DictationPending;
    PleaseReviewCheckBox.Checked:=PleaseReview;
    NoteGroupBox.Caption:='Note Text  (Author='+Author+')';
    NoteStringStream.Position:=0;
    NoteRichEdit.Lines.LoadFromStream(NoteStringStream);
    //Don't forget the caption on the note selector box too
    //NotesListView.ItemFocused.Caption:=DatetoStr(NoteDateTime);
    NotesListView.ItemFocused.SubItems[0]:=Description;

    if NewNote or Modded or OpenforEditing then
      EnableNoteEditingControls
    else
      DisableNoteEditingControls;

    //Since we updated the editing controls, modified is true now
    //We need to reset them to false as content of the fields has not changed
    DescriptionEdit.Modified:=False;
    NoteRichEdit.Modified:=False;
  end;
end;

procedure TPatientExplorerForm.NewNoteClick(Sender: TObject);
var NoteRecordPtr: PNoteRecord;
begin
  new(NoteRecordPtr);
  with NoteRecordPtr^ do
  begin
    PatientID:=PPatientRecord(MainForm.PatientListView.ItemFocused.Data)^.PatientID;
    Description:='Blank Note';
    NoteDateTime:=Now;
    NoteStringStream:=TStringStream.Create('');
    DictationPending:=False;
    PleaseReview:=False;
    Author:=MainForm.SQLConnection.Params.Values['User_Name'];
    NewNote:=True;
    Modded:=True;
    OpenforEditing:=True;

    with NotesListView.Items.Insert(0) do
    begin
      Caption:=FormatDateTime('dd mmm yy', NoteDateTime);
      SubItems.Add(Description);
      Data:=TObject(NoteRecordPtr);
    end;
  end;

  //We just inserted a new note at the top of the list, now we must select it
  NotesListView.Selected:=NotesListView.TopItem;
  NotesListView.ItemFocused:=NotesListView.TopItem;

  NotesListViewClick(Sender);
end;

procedure TPatientExplorerForm.NewNotefromTemplateClick(Sender: TObject);
begin
  with TemplateManagerForm do
  begin
    Import:=True;
    ShowModal;
  end;
end;

procedure TPatientExplorerForm.EditNoteClick(Sender: TObject);
begin
  if NotesListView.Items.Count<=0 then Exit;
  if NotesListView.ItemFocused=nil then Exit;
  if not Assigned(NotesListView.ItemFocused.Data) then Exit;

  if PNoteRecord(NotesListView.ItemFocused.Data)^.Author<>
    MainForm.SQLConnection.Params.Values['User_Name'] then
  begin
    MessageDlg('Unable to edit note - You are not the author of this note', mtError, [mbOk], 0);
    Exit;
  end;

  PNoteRecord(NotesListView.ItemFocused.Data)^.OpenforEditing:=True;

  EnableNoteEditingControls;
end;

procedure TPatientExplorerForm.DeleteNoteClick(Sender: TObject);
begin
  if NotesListView.Items.Count<=0 then Exit;
  if NotesListView.ItemFocused=nil then Exit;
  if not Assigned(NotesListView.ItemFocused.Data) then Exit;

  if PNoteRecord(NotesListView.ItemFocused.Data)^.Author<>
    MainForm.SQLConnection.Params.Values['User_Name'] then
  begin
    MessageDlg('Unable to delete - You are not the author of this note', mtError, [mbOk], 0);
    Exit;
  end;

  if MessageDlg('Are you sure you want to delete the selected note?',
                mtConfirmation, [mbYes, mbNo], 0) <> mrYes then Exit;

  if not PNoteRecord(NotesListView.ItemFocused.Data)^.NewNote then
  begin
    //if is new note, it has never been saved to DB so no need for delete query

    //Efficient - No db refresh necessary since item is removed from list
    with SQLQuery do
    begin
      SQL.Clear;
      SQL.Add('delete from notes where noteid='+
              inttostr(PNoteRecord(NotesListView.ItemFocused.Data)^.NoteID) );
      ExecSQL;
    end;
  end;

  //now remove last trace of note record from list
  PNoteRecord(NotesListView.ItemFocused.Data)^.NoteStringStream.Free;
  Dispose(PNoteRecord(NotesListView.ItemFocused.Data));
  NotesListView.ItemFocused.Delete;

  //A note has been deleted from the list.  It is possible that the PreviousItem
  //pointer pointed towards the deleted note.  If this is the case and Redraw
  //is called, an access error will trip.  The PreviousItem pointer must be reset.

  PreviousItem:=nil;

  NotesListViewClick(Sender);
end;

procedure TPatientExplorerForm.SaveNoteClick(Sender: TObject);
begin
  if NotesListView.Items.Count<=0 then Exit;
  if NotesListView.ItemFocused=nil then Exit;
  if not Assigned(NotesListView.ItemFocused.Data) then Exit;

  if not (PNoteRecord(NotesListView.ItemFocused.Data)^.NewNote or PNoteRecord(NotesListView.ItemFocused.Data)^.Modded or DescriptionEdit.Modified or NoteRichEdit.Modified or (PNoteRecord(NotesListView.ItemFocused.Data)^.DictationPending<>DictationPendingCheckBox.Checked) or (PNoteRecord(NotesListView.ItemFocused.Data)^.PleaseReview<>PleaseReviewCheckBox.Checked)) then
  begin
    MessageDlg('No need to save - There have been no modifications to the note! ', mtError, [mbOk], 0);
    Exit;
  end;

  if not (PNoteRecord(NotesListView.ItemFocused.Data)^.NewNote) and (PNoteRecord(NotesListView.ItemFocused.Data)^.Author<>
    MainForm.SQLConnection.Params.Values['User_Name']) then
  begin
    MessageDlg('Unable to overwrite note - You are not the author of this note', mtError, [mbOk], 0);
    Exit;
  end;

  if DescriptionEdit.Text='' then
  begin
    MessageDlg('You must enter a description to save this note!', mtError, [mbOk], 0);
    Exit;
  end;

  if PNoteRecord(NotesListView.ItemFocused.Data)^.NewNote then
  begin
    //important to add edited string before creating memory stream
    NoteRichEdit.Lines.Append('');
    NoteRichEdit.Lines.Append('[ Note created on '+FormatDateTime('d mmm yy', Now)+' '+FormatDateTime('hhnn', Now)+' by '+MainForm.SQLConnection.Params.Values['User_Name']+' ]');
  end
  else
  begin
    //important to add edited string before creating memory stream
    NoteRichEdit.Lines.Append('');
    NoteRichEdit.Lines.Append('[ Note edited on '+FormatDateTime('d mmm yy', Now)+' '+FormatDateTime('hhnn', Now)+' by '+MainForm.SQLConnection.Params.Values['User_Name']+' ]');
  end;

  DisableNoteEditingControls;

  DateSeparator := '/';
  ShortDateFormat := 'yyyy/mm/dd';
  LongTimeFormat:='hh:nn:ss';

  with SQLQuery do
  begin
    SQL.Clear;
    Params.Clear;

    with PNoteRecord(NotesListView.ItemFocused.Data)^ do
    begin
      Description:=DescriptionEdit.Text;
      DictationPending:=DictationPendingCheckBox.Checked;
      PleaseReview:=PleaseReviewCheckBox.Checked;
      NoteStringStream.Position:=0;
      NoteRichEdit.Lines.SaveToStream(NoteStringStream);
      //No need to update patientid as will remain constant
      //NoteDateTime refer to creation datetime and do not need updated

      if NewNote then
      begin
        SQL.Text:='insert into notes (patientid, description, notedatetime, note, author, dictationpending, pleasereview) values (:patientid, :description, :notedatetime, :note, :author, :dictationpending, :pleasereview)';
        Params[0].AsString:=inttostr(PatientID);
        Params[1].AsString:=Description;
        Params[2].AsString:=DateTimetoStr(NoteDateTime);
        Params[3].LoadFromStream(NoteStringStream, ftFmtMemo);
        Params[4].AsString:=Author;
        Params[5].AsString:=IntToStr(Integer(DictationPending));
        Params[6].AsString:=IntToStr(Integer(PleaseReview));
        ExecSql;

        //Now update LastSeen date in main patients DB
        SQL.Clear;
        Params.Clear;
        SQL.Text:='update patients set lastseendatetime=:lastseendatetime where patientid=:patientid';
        with PPatientRecord(MainForm.PatientListView.ItemFocused.Data)^ do
        begin
          LastSeenDateTime:=Now;
          Params[0].AsString:=DatetoStr(LastSeenDateTime);
          Params[1].AsString:=InttoStr(PatientID);
        end;
        //Now update selected item to reflect changes
        MainForm.PatientListViewClick(Sender);
        ExecSql;
      end
      else
      begin //now update db only
        SQL.Text:='update notes set description=:description, note=:note, dictationpending=:dicationpending, pleasereview=:pleasereview where noteid=:noteid';
        Params[0].AsString:=Description;
        Params[1].LoadFromStream(NoteStringStream, ftFmtMemo);
        Params[2].AsString:=IntToStr(Integer(DictationPending));
        Params[3].AsString:=IntToStr(Integer(PleaseReview));
        Params[4].AsString:=InttoStr(NoteID);
        ExecSql;
      end;

      //Since the note has been saved, we need to update the internal vars and
      //editing controls to reflect that the note is no longer modified
      NewNote:=False; //We will set NewNote to false just to be safe in all cases
      Modded:=False;
      OpenforEditing:=False;
      DescriptionEdit.Modified:=False;
      NoteRichEdit.Modified:=False;

    end; //with PNoteRecord
  end; //with SQLQuery

  PreviousItem:=nil;
  NotesListViewClick(Sender);
end;

procedure TPatientExplorerForm.RevertNoteClick(Sender: TObject);
begin
  if NotesListView.Items.Count<=0 then Exit;
  if NotesListView.ItemFocused=nil then Exit;
  if not Assigned(NotesListView.ItemFocused.Data) then Exit;

  if PNoteRecord(NotesListView.ItemFocused.Data)^.NewNote then
  begin
    MessageDlg('Nothing to revert - This is a new note! ', mtError, [mbOk], 0);
    Exit;
  end;

  if not (PNoteRecord(NotesListView.ItemFocused.Data)^.Modded or DescriptionEdit.Modified or NoteRichEdit.Modified or (PNoteRecord(NotesListView.ItemFocused.Data)^.DictationPending<>DictationPendingCheckBox.Checked) or (PNoteRecord(NotesListView.ItemFocused.Data)^.PleaseReview<>PleaseReviewCheckBox.Checked)) then
  begin
    MessageDlg('No need to revert - There have been no modifications to the note! ', mtError, [mbOk], 0);
    Exit;
  end;

  if MessageDlg('Reload template and lose changes?', mtConfirmation, [mbYes, mbNo], 0) = mrNo then Exit;

  with SQLQuery do
  begin
    SQL.Clear;

    SQL.Add('select * from notes where noteid='''+InttoStr(PNoteRecord(NotesListView.ItemFocused.Data)^.NoteID)+'''');

    Open;
    First;
    with PNoteRecord(NotesListView.ItemFocused.Data)^ do
    begin
      //NoteID:=FieldByName('noteid').AsInteger;
      Description:=FieldByName('description').AsString;
      NoteStringStream:=TStringStream.Create(FieldByName('note').AsString);
      Author:=FieldByName('author').AsString;
      NewNote:=False;
      Modded:=False;
      OpenforEditing:=False;
    end;
    //no need for next since only one record
    Close;
  end;

  PreviousItem:=nil; //this will force a screen redraw
  NotesListViewClick(Sender);
end;

procedure TPatientExplorerForm.RefreshNotesDBClick(Sender: TObject);
var NoteRecordPtr: PNoteRecord;
    SelectedNoteIDHolder, i: integer;
    Save_Cursor: TCursor;
begin
  //Change the cursor to an hour glass with SQL caption
  Save_Cursor:=Screen.Cursor;
  Screen.Cursor:=crSQLWait;

  //Remember which note was selected because forgotten during refresh.  Set the
  //note place holder to -1 in case no note had ever been selected.  Also check
  //to make sure that the item has a pointer associated with it to prevent an
  //access error.

  SelectedNoteIDHolder:=-1;
  if NotesListView.ItemFocused<>nil then
  begin
    if Assigned(NotesListView.ItemFocused.Data) then
      SelectedNoteIDHolder:=PNoteRecord(NotesListView.ItemFocused.Data)^.NoteID;
  end;

  //Suspend screen updates to improve speed
  NotesListView.Items.BeginUpdate;

  CleanUpNotesListView;

  with SQLQuery do
  begin
    SQL.Clear;

    SQL.Add('select * from notes where patientid='+InttoStr(PPatientRecord(MainForm.PatientListView.ItemFocused.Data)^.PatientID)+' order by notedatetime desc');

    Open;
    First;
    while (not EOF) do
    begin
      new(NoteRecordPtr);
      with NoteRecordPtr^ do
      begin
        NoteID:=FieldByName('noteid').AsInteger;
        PatientID:=FieldByName('patientid').AsInteger;
        Description:=FieldByName('description').AsString;
        NoteDateTime:=StrtoDateTime(FieldByName('notedatetime').AsString);
        NoteStringStream:=TStringStream.Create(FieldByName('note').AsString);
        Author:=FieldByName('author').AsString;
        DictationPending:=Boolean(FieldByName('dictationpending').AsInteger);
        PleaseReview:=Boolean(FieldByName('pleasereview').AsInteger);
        NewNote:=False;
        Modded:=False;
        OpenforEditing:=False;
        with NotesListView.Items.Add do
        begin
          Caption:=FormatDateTime('dd mmm yy', NoteDateTime);
          SubItems.Add(Description);
          Data:=TObject(NoteRecordPtr);
        end;
      end;
      Next;
    end;
    Close;
  end;
  NotesListView.Items.EndUpdate;
  Screen.Cursor:=Save_Cursor;

  //If there was no previously selected note or if there are no notes found,
  //the editing controls need reset.  If there happens to be a note but none
  //previously selected, this item will be selected and the screen updated.

  if (SelectedNoteIDHolder=-1) or (NotesListView.Items.Count<=0) then
  begin
    NotesListViewClick(Sender);
    Exit;
  end;

  //At this point, there are notes and we have remembered the note that was
  //previously selected.  We will search through the notes, find and select it.

  for i:=0 to NotesListView.Items.Count-1 do
  begin
    if PNoteRecord(NotesListView.Items[i].Data)^.NoteID = SelectedNoteIDHolder then
    begin
      NotesListView.Selected := NotesListView.Items[i];
      NotesListView.ItemFocused := NotesListView.Items[i];
      Break;
    end;
  end;

  //If the note was found or in the rare case it was not, the redraw routines
  //will clean up.

  NotesListViewClick(Sender);
end;

{-----------------------------------------------------
Scales DB - For tracking Rating Scales
------------------------------------------------------}

procedure TPatientExplorerForm.CleanUpScalesListView;
var i: integer;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -