public function test_save()
 {
     $label = local_mail_label::create(201, 'name', 'red');
     $label->save('changed', 'green');
     $this->assertEquals('changed', $label->name());
     $this->assertEquals('green', $label->color());
     $this->assertLabel($label);
 }
Esempio n. 2
0
 }
 // Create form
 $mform = new mail_labels_form($url, $customdata);
 $mform->set_data($customdata);
 if ($data = $mform->get_data()) {
     if (isset($data->submitbutton)) {
         $newlabel = false;
         $data->newlabelname = trim(clean_param($data->newlabelname, PARAM_TEXT));
         $data->newlabelname = preg_replace('/\\s+/', ' ', $data->newlabelname);
         $validcolor = (!$data->newlabelcolor or in_array($data->newlabelcolor, $colors));
         $repeatedname = false;
         foreach ($labels as $label) {
             $repeatedname = $repeatedname || $label->name() === $data->newlabelname;
         }
         if (!$repeatedname and !empty($data->newlabelname) and $validcolor) {
             $newlabel = local_mail_label::create($USER->id, $data->newlabelname, $data->newlabelcolor);
             if (!isset($data->labelid)) {
                 $data->labelid = array();
             }
             $data->labelid[$newlabel->id()] = 1;
         }
         if ($messageid) {
             $message = local_mail_message::fetch($messageid);
             if (!$message or !$message->viewable($USER->id) or $message->deleted($USER->id)) {
                 print_error('nomessages', 'local_mail');
             }
             if (isset($data->labelid)) {
                 $data->labelid = clean_param_array($data->labelid, PARAM_INT);
                 $labels = local_mail_label::fetch_user($USER->id);
                 foreach ($labels as $label) {
                     if ($data->labelid[$label->id()]) {
    public function test_set_unread() {
        $label = local_mail_label::create(201, 'label');
        $message = local_mail_message::create(201, 101);
        $message->add_label($label);
        $message->set_starred(201, true);

        $message->set_unread(201, true);

        $this->assertTrue($message->unread(201));
        $this->assertIndex(201, 'drafts', 0, $message->time(), $message->id(), true);
        $this->assertIndex(201, 'starred', 0, $message->time(), $message->id(), true);
        $this->assertIndex(201, 'course', $message->course()->id, $message->time(), $message->id(), true);
        $this->assertMessage($message);

        $message->set_unread(201, false);

        $this->assertFalse($message->unread(201));
        $this->assertIndex(201, 'drafts', 0, $message->time(), $message->id(), false);
        $this->assertIndex(201, 'starred', 0, $message->time(), $message->id(), false);
        $this->assertIndex(201, 'course', $message->course()->id, $message->time(), $message->id(), false);
        $this->assertMessage($message);
    }
Esempio n. 4
0
function local_mail_newlabel($messages, $labelname, $labelcolor, $data)
{
    global $CFG, $USER;
    $error = '';
    $labelname = trim($labelname);
    $labelname = preg_replace('/\\s+/', ' ', $labelname);
    $colors = local_mail_label::valid_colors();
    $validcolor = (!$labelcolor or in_array($labelcolor, $colors));
    $labels = local_mail_label::fetch_user($USER->id);
    $repeatedname = false;
    foreach ($labels as $label) {
        $repeatedname = $repeatedname || $label->name() === $labelname;
    }
    if (!$repeatedname) {
        if (!empty($labelname) and $validcolor) {
            $newlabel = local_mail_label::create($USER->id, $labelname, $labelcolor);
            foreach ($messages as $message) {
                if ($message->viewable($USER->id) and !$message->deleted($USER->id)) {
                    $message->add_label($newlabel);
                }
            }
        } else {
            $error = empty($labelname) ? get_string('erroremptylabelname', 'local_mail') : get_string('errorinvalidcolor', 'local_mail');
        }
    } else {
        $error = get_string('errorrepeatedlabelname', 'local_mail');
    }
    return array('msgerror' => $error, 'info' => '', 'html' => '', 'redirect' => $CFG->wwwroot . '/local/mail/view.php?' . implode('&', $data));
}