<?php

  /*

      remark.php

      Contains code to add a remark to the database.

      Changelog:
        2005-07-03 arne_sf: Replaced all instances of column name 'user' for table tbl_Times with 'userid'
        2005-07-03 arne_sf: Replaced all instances of column name 'user' for table tbl_UserSites with 'userid'
                
      ## PAGE CONTAINS HANDYANDY MYSQL MODS ##
      ## Copyright (C) 2005 Andy Deakin (handyandy.org.uk) ##

      ----

      Copyright (C) 2003  Central Manchester CLC
      Copyright (C) 2003  David Thorne (davethorne@gmail.com)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */
if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__, '.php')) !== false) {
  header("location: index.php");
}
  require_once 'system/db.php';
  require_once 'system/user_preferences.php';
  require_once 'system/mail.php';

  function makeremark($id,$remark,$confidential,$mailto, $time='') {
    global $mailtemplate;
    // If a remark was entered, then enter this into the database
    if ($remark != '') {
      if ($confidential == 'true') { $confidential = 1; } else { $confidential =0; }
      if ($time == '') {
          $sql = "INSERT INTO tbl_Remarks (issue,reportedby,reportedon,remark,confidential) VALUES ($id," . $_SESSION['_id'] . ',' . time() . ",'$remark',$confidential);";
      } else {
          $sql = "INSERT INTO tbl_Remarks
          (issue,reportedby,reportedon,remark,confidential,time) VALUES ($id," .
          $_SESSION['_id'] . ',' . time() . ",'$remark',$confidential,
          '$time');";
      }
      db_send($sql);

      // Send email if requested
      $recipients = '';
      foreach ($mailto as $person) {
      $pers[$person] = $person;
    }
      foreach ($pers as $key => $person) {
    //echo "pers: $person<br>\n";
        $user_prefs = get_user_prefs($person);
        $user_details = get_user_details($person);
        if ($person!=$_SESSION['_id'] && $user_prefs['email-remark'] == 'true' && $user_details['email']!='') {
          $sql = "SELECT tbl_UserTypes.type AS usertypename
        FROM tbl_UserTypes,tbl_UserSites, tbl_Issues
        WHERE tbl_UserSites.userid=$person
        AND tbl_UserSites.usertype=tbl_UserTypes.id
        AND tbl_Issues.id = $id
        AND tbl_UserSites.site=tbl_Issues.site";
      $usertypes = db_recordset($sql);
          $usertype = $usertypes[0][usertypename];
          if ($confidential != 1 || ($usertype!='Client' && $usertype!='Site Contact')) {
//             $recipients .= $user_details[email] . ",";
      send_mail('remark',$user_prefs['language'],
        $user_details[email],$id,$_SESSION['_name'],$remark);
      //echo "send_mail(".$mailtemplate['remark'].",".$user_prefs['language'].",".
      //  $user_details['email'].",".$id.",".$_SESSION['_name'].",".$remark.")<br>\n";
          }
        }
      }
    }
  }


?>
