[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/notes/ -> lib.php (source)

   1  <?php // $Id: lib.php,v 1.7.2.2 2008/02/13 17:01:45 skodak Exp $
   2  
   3  /**
   4   * Library of functions and constants for notes
   5   */
   6  
   7  /**
   8   * Constants for states.
   9   */
  10  define('NOTES_STATE_DRAFT', 'draft');
  11  define('NOTES_STATE_PUBLIC', 'public');
  12  define('NOTES_STATE_SITE', 'site');
  13  
  14  /**
  15   * Constants for note parts (flags used by note_print and note_print_list).
  16   */
  17  define('NOTES_SHOW_FULL', 0x07);
  18  define('NOTES_SHOW_HEAD', 0x02);
  19  define('NOTES_SHOW_BODY', 0x01);
  20  define('NOTES_SHOW_FOOT', 0x04);
  21  
  22  /**
  23   * Retrieves a list of note objects with specific atributes.
  24   *
  25   * @param int    $courseid id of the course in which the notes were posted (0 means any)
  26   * @param int    $userid id of the user to which the notes refer (0 means any)
  27   * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
  28   * @param int    $author id of the user who modified the note last time (0 means any)
  29   * @param string $order an order to sort the results in
  30   * @param int    $limitfrom number of records to skip (offset)
  31   * @param int    $limitnum number of records to fetch
  32   * @return array of note objects
  33   */
  34  function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='lastmodified DESC', $limitfrom=0, $limitnum=0) {
  35      // setup filters
  36      $selects = array();
  37      if($courseid) {
  38          $selects[] = 'courseid=' . $courseid;
  39      }
  40      if($userid) {
  41          $selects[] = 'userid=' . $userid;
  42      }
  43      if($author) {
  44          $selects[] = 'usermodified=' . $author;
  45      }
  46      if($state) {
  47          $selects[] = "publishstate='$state'";
  48      }
  49      $selects[] = "module='notes'";
  50      $select = implode(' AND ', $selects);
  51      $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
  52      // retrieve data
  53      $rs =& get_recordset_select('post', $select, $order, $fields, $limitfrom, $limitnum);
  54      return recordset_to_array($rs);
  55  }
  56  
  57  /**
  58   * Retrieves a note object based on its id.
  59   *
  60   * @param int    $note_id id of the note to retrieve
  61   * @return note object
  62   */
  63  function note_load($note_id) {
  64      $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
  65      return get_record_select('post', "id=$note_id AND module='notes'", $fields);
  66  }
  67  
  68  /**
  69   * Saves a note object. The note object is passed by reference and its fields (i.e. id)
  70   * might change during the save.
  71   *
  72   * @param note   $note object to save
  73   * @return boolean true if the object was saved; false otherwise
  74   */
  75  function note_save(&$note) {
  76      global $USER;
  77      // setup & clean fields
  78      $note->module = 'notes';
  79      $note->lastmodified = time();
  80      $note->usermodified = $USER->id;
  81      if(empty($note->format)) {
  82          $note->format = FORMAT_PLAIN;
  83      }
  84      if(empty($note->publishstate)) {
  85          $note->publishstate = NOTES_STATE_PUBLIC;
  86      }
  87      // save data
  88      if(empty($note->id)) {
  89          // insert new note
  90          $note->created = $note->lastmodified;
  91          if($id = insert_record('post', $note)) {
  92              $note->id = $id;
  93              $result = true;
  94          } else {
  95              $result = false;
  96          }
  97      } else {
  98          // update old note
  99          $result = update_record('post', $note);
 100      }
 101      unset($note->module);
 102      return $result;
 103  }
 104  
 105  /**
 106   * Deletes a note object based on its id.
 107   *
 108   * @param int    $note_id id of the note to delete
 109   * @return boolean true if the object was deleted; false otherwise
 110   */
 111  function note_delete($noteid) {
 112      return delete_records_select('post', "id=$noteid AND module='notes'");
 113  }
 114  
 115  /**
 116   * Converts a state value to its corespondent name
 117   *
 118   * @param string  $state state value to convert
 119   * @return string corespondent state name
 120   */
 121  function note_get_state_name($state) {
 122      // cache state names
 123      static $states;
 124      if (empty($states)) {
 125          $states = note_get_state_names();
 126      }
 127      return @$states[$state];
 128  }
 129  
 130  /**
 131   * Returns an array of mappings from state values to state names
 132   *
 133   * @return array of mappings
 134   */
 135  function note_get_state_names() {
 136      return array(
 137          NOTES_STATE_DRAFT => get_string('personal', 'notes'),
 138          NOTES_STATE_PUBLIC => get_string('course', 'notes'),
 139          NOTES_STATE_SITE => get_string('site', 'notes'),
 140      );
 141  }
 142  
 143  /**
 144   * Prints a note object
 145   *
 146   * @param note  $note the note object to print
 147   * @param int   $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
 148   */
 149  function note_print($note, $detail = NOTES_SHOW_FULL) {
 150  
 151      global $CFG, $USER;
 152      if (!$user = get_record('user','id',$note->userid)) {
 153          debugging("User $note->userid not found");
 154          return;
 155      }
 156      if (!$author = get_record('user','id',$note->usermodified)) {
 157          debugging("User $note->usermodified not found");
 158          return;
 159      }
 160      $context = get_context_instance(CONTEXT_COURSE, $note->courseid);
 161      $sitecontext = get_context_instance(CONTEXT_SYSTEM);
 162  
 163      $authoring = new object;
 164      $authoring->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$author->id.'&amp;course='.$note->courseid.'">'.fullname($author).'</a>';
 165      $authoring->date = userdate($note->lastmodified);
 166  
 167      echo '<div class="notepost '. $note->publishstate . 'notepost' .
 168          ($note->usermodified == $USER->id ? ' ownnotepost' : '')  .
 169          '" id="note-'. $note->id .'">';
 170  
 171      // print note head (e.g. author, user refering to, etc)
 172      if($detail & NOTES_SHOW_HEAD) {
 173          echo '<div class="header">';
 174          echo '<div class="user">';
 175          print_user_picture($user, $note->courseid, $user->picture);
 176          echo fullname($user) . '</div>';
 177          echo '<div class="info">' .
 178              get_string('bynameondate', 'notes', $authoring) .
 179              ' (' . get_string('created', 'notes') . ': ' . userdate($note->created) . ')</div>';
 180          echo '</div>';
 181      }
 182  
 183      // print note content
 184      if($detail & NOTES_SHOW_BODY) {
 185          echo '<div class="content">';
 186          echo format_text($note->content, $note->format);
 187          echo '</div>';
 188      }
 189  
 190      // print note options (e.g. delete, edit)
 191      if($detail & NOTES_SHOW_FOOT) {
 192          if (has_capability('moodle/notes:manage', $sitecontext) && $note->publishstate == NOTES_STATE_SITE ||
 193              has_capability('moodle/notes:manage', $context) && ($note->publishstate == NOTES_STATE_PUBLIC || $note->usermodified == $USER->id)) {
 194              echo '<div class="footer"><p>';
 195              echo '<a href="'.$CFG->wwwroot.'/notes/edit.php?note='.$note->id. '">'. get_string('edit') .'</a> | ';
 196              echo '<a href="'.$CFG->wwwroot.'/notes/delete.php?note='.$note->id. '">'. get_string('delete') .'</a>';
 197              echo '</p></div>';
 198          }
 199      }
 200      echo '</div>';
 201  }
 202  
 203  /**
 204   * Prints a list of note objects
 205   *
 206   * @param array  $notes array of note objects to print
 207   * @param int   $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
 208   */
 209  function note_print_list($notes, $detail = NOTES_SHOW_FULL) {
 210  
 211      /// Start printing of the note
 212      echo '<div class="notelist">';
 213      foreach ($notes as $note) {
 214          note_print($note, $detail);
 215      }
 216      echo '</div>';
 217  }
 218  
 219  /**
 220   * Retrieves and prints a list of note objects with specific atributes.
 221   *
 222   * @param string  $header HTML to print above the list
 223   * @param int     $addcourseid id of the course for the add notes link (0 hide link)
 224   * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
 225   * @param int     $courseid id of the course in which the notes were posted (0 means any)
 226   * @param int     $userid id of the user to which the notes refer (0 means any)
 227   * @param string  $state state of the notes (i.e. draft, public, site) ('' means any)
 228   * @param int     $author id of the user who modified the note last time (0 means any)
 229   */
 230  function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0)
 231  {
 232      global $CFG;
 233      if ($header) {
 234          echo '<h3 class="notestitle">' . $header . '</h3>';
 235          echo '<div class="notesgroup">';
 236      }
 237      if ($addcourseid) {
 238          if ($userid) {
 239             echo '<p><a href="'. $CFG->wwwroot .'/notes/add.php?course=' . $addcourseid . '&amp;user=' . $userid . '&amp;state=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
 240          } else {
 241             echo '<p><a href="'. $CFG->wwwroot .'/user/index.php?id=' . $addcourseid. '">' . get_string('addnewnoteselect', 'notes') . '</a></p>';
 242          }
 243      }
 244      if ($viewnotes) {
 245          $notes =& note_list($courseid, $userid, $state, $author);
 246          if ($notes) {
 247              note_print_list($notes);
 248          }
 249      } else {
 250          echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
 251      }
 252      if ($header) {
 253          echo '</div>';  // notesgroup
 254      }
 255  }
 256  
 257  /**
 258   * Delete all notes about users in course-
 259   * @param int $courseid
 260   * @return bool success
 261   */
 262  function note_delete_all($courseid) {
 263      return delete_records('post', 'module', 'notes', 'courseid', $courseid);
 264  }
 265  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7