/**
  * This function takes a list of records and adds them to an array that is later processed in javascript.
  *
  * @param array $tablerows This array will be appended with all of the requested records (passed by ref).
  * @param int $start This is the record we should begin processing at (wasteful, should be changed).
  *
  */
 public function get_table_rows(&$tablerows, $start)
 {
     global $DB;
     foreach ($this->records as $row) {
         $ids = $this->folder == 'sent' ? $row->recipients : $row->useridfrom;
         $names = block_course_message_map_ids_to_names($ids, $this->folder);
         // Time format is handled in javascript.
         $timestring = $row->timesent;
         $status = $row->timeread != 0 ? 1 : 0;
         if ($this->folder == 'sent') {
             $tablerows[] = array('id' => $row->id, 'to' => $names, 'subject' => $row->subject, 'sent' => $timestring, 'status' => STATUS_READ);
         } else {
             $tablerows[] = array('id' => $row->id, 'from' => $names, 'subject' => $row->subject, 'received' => $timestring, 'status' => $status);
         }
     }
 }
 /**
  * This function tests the block_course_message_map_ids_to_names() function.  This
  * is a fairly important function, since it has to handle decoding both user ids and
  * group ids.
  *
  */
 public function test_map_ids_to_names()
 {
     // Test 1: viewing mail in inbox (single user).
     $ids = $this->friend->id;
     $folder = 'inbox';
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, "Craig's Friend");
     // Test 2: viewing mail in inbox (array or multiple users) -> straight return.
     $ids = "[\"{$this->friend->id}\"]";
     // JSON decodes as array.
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, "");
     $ids = "[\"{$this->friend->id}\", \"{$this->craig->id}\"]";
     // Multiple recipients.
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, "");
     // Test 3: viewing sent message (to: single user).
     $ids = "[\"{$this->friend->id}\"]";
     // This works for 'sent'.
     $folder = 'sent';
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, "Craig's Friend");
     // Test 4: viewing sent message (to: multiple users).
     $ids = "[\"{$this->friend->id}\", \"{$this->craig->id}\", \"{$this->martha->id}\", \"2\"]";
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, "Craig's Friend, Craig Jamieson, Martha Stein, Admin User");
     // Test 5: viewing sent message (to: multiple users + group).
     $ids = "[\"{$this->friend->id}\", \"{$this->craig->id}\", \"g{$this->testgroupid}\", \"{$this->martha->id}\", \"2\"]";
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, "Craig's Friend, Craig Jamieson, {$this->testgroupname}, Martha Stein, Admin User");
     // Test out all instructors.
     $ids = '["i1"]';
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, get_string('allinstructors', BLOCK_CM_LANG_TABLE));
     // Test out all students.
     $ids = '["s1"]';
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, get_string('allstudents', BLOCK_CM_LANG_TABLE));
     // Test all instructors + a student.
     $ids = "[\"{$this->friend->id}\", \"i1\"]";
     $names = block_course_message_map_ids_to_names($ids, $folder);
     $this->assertEquals($names, "Craig's Friend, " . get_string('allinstructors', BLOCK_CM_LANG_TABLE));
 }
 /**
  * This function writes out the message div so the message can be viewed.
  *
  * @param object $record This is the DB record for the message to write out
  * @param string $outputbuffer The output html code will be written here.
  *
  */
 private function write_message_div($record, &$outputbuffer)
 {
     global $DB, $USER;
     $timesent = $record->timesent;
     $time = userdate($timesent, '%H:%M');
     $dates = usergetdate($timesent);
     $userfrom = block_course_message_map_ids_to_names($record->useridfrom, 'inbox');
     $userto = block_course_message_map_ids_to_names($record->recipients, 'sent');
     $outputbuffer .= "<div class='message_div_bar'><div class='message_bar_to'><span class='label'>To:</span>\n                          <span class='field'> {$userto}</span></div>";
     if ($record->carboncopy !== null) {
         $carboncopy = block_course_message_map_ids_to_names($record->carboncopy, 'sent');
         $outputbuffer .= "<div class='message_bar_to'><span class='label'>CC:</span>\n                              <span class='field'> {$carboncopy}</span></div>";
     }
     $outputbuffer .= "<div class='message_bar_from'><span class='label'>From:</span><span class='field'> {$userfrom}</span>\n                          </div><div class='message_bar_time'>{$time} {$dates['month']} {$dates['mday']}</div>";
     $outputbuffer .= "<div class='clear'></div></div>";
     $outputbuffer .= "<div class='message_container";
     if ($USER->id == $record->useridfrom) {
         $outputbuffer .= " usersent'>";
     } else {
         $outputbuffer .= " userrecieved'>";
     }
     // Message has already been cleaned when sent.
     $outputbuffer .= "<div class='message_div_body'>{$record->message}</div>";
     $this->process_attachment($record, $outputbuffer);
     // Close the message container div.
     $outputbuffer .= "</div>";
 }