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);
 }
Ejemplo n.º 2
0
function local_mail_extend_navigation($root) {
    global $CFG, $COURSE, $PAGE, $SESSION, $SITE, $USER;

    if (!get_config('local_mail', 'version')) {
        return;
    }

    $courses = local_mail_get_my_courses();

    $count = local_mail_message::count_menu($USER->id);

    // My mail

    $text = get_string('mymail', 'local_mail');
    if (!empty($count->inbox)) {
        $text .= ' (' . $count->inbox . ')';
    }
    $node = navigation_node::create($text, null, navigation_node::TYPE_ROOTNODE);
    if (!empty($count->inbox)) {
        $node->add_class('local_mail_new_messages');
    }
    $child = $root->add_node($node, 'mycourses');
    $child->add_class('mail_root');

    // Compose

    $text = get_string('compose', 'local_mail');
    $url = new moodle_url('/local/mail/compose.php');
    $urlrecipients = new moodle_url('/local/mail/recipients.php');

    if ($PAGE->url->compare($url, URL_MATCH_BASE) or
        $PAGE->url->compare($urlrecipients, URL_MATCH_BASE)) {
        $url->param('m', $PAGE->url->param('m'));
    } else {
        $url = new moodle_url('/local/mail/create.php');
        if ($COURSE->id != $SITE->id) {
            $url->param('c', $COURSE->id);
            $url->param('sesskey', sesskey());
        }
    }

    $node->add(s($text), $url);

    // Inbox

    $text = get_string('inbox', 'local_mail');
    if (!empty($count->inbox)) {
        $text .= ' (' . $count->inbox . ')';
    }
    $url = new moodle_url('/local/mail/view.php', array('t' => 'inbox'));
    $child = $node->add(s($text), $url);
    $child->add_class('mail_inbox');

    // Starred

    $text = get_string('starredmail', 'local_mail');
    $url = new moodle_url('/local/mail/view.php', array('t' => 'starred'));
    $node->add(s($text), $url);

    // Drafts

    $text = get_string('drafts', 'local_mail');
    if (!empty($count->drafts)) {
        $text .= ' (' . $count->drafts . ')';
    }
    $url = new moodle_url('/local/mail/view.php', array('t' => 'drafts'));
    $child = $node->add(s($text), $url);
    $child->add_class('mail_drafts');

    // Sent

    $text = get_string('sentmail', 'local_mail');
    $url = new moodle_url('/local/mail/view.php', array('t' => 'sent'));
    $node->add(s($text), $url);

    // Courses

    if ($courses) {
        $text = get_string('courses', 'local_mail');
        $nodecourses = $node->add($text, null, navigation_node::TYPE_CONTAINER);
        foreach ($courses as $course) {
            $text = $course->shortname;
            if (!empty($count->courses[$course->id])) {
                $text .= ' (' . $count->courses[$course->id] . ')';
            }
            $params = array('t' => 'course', 'c' => $course->id);
            $url = new moodle_url('/local/mail/view.php', $params);
            $child = $nodecourses->add(s($text), $url);
            $child->hidden = !$course->visible;
            $child->add_class('mail_course_'.$course->id);
        }
    }

    // Labels

    $labels = local_mail_label::fetch_user($USER->id);
    if ($labels) {
        $text = get_string('labels', 'local_mail');
        $nodelabels = $node->add($text, null, navigation_node::TYPE_CONTAINER);
        foreach ($labels as $label) {
            $text = $label->name();
            if (!empty($count->labels[$label->id()])) {
                $text .= ' (' . $count->labels[$label->id()] . ')';
            }
            $params = array('t' => 'label', 'l' => $label->id());
            $url = new moodle_url('/local/mail/view.php', $params);
            $child = $nodelabels->add(s($text), $url);
            $child->add_class('mail_label_'.$label->id());
        }
    }

    // Trash

    $text = get_string('trash', 'local_mail');
    $url = new moodle_url('/local/mail/view.php', array('t' => 'trash'));
    $node->add(s($text), $url);

    // Preferences

    $text = get_string('preferences');
    $url = new moodle_url('/local/mail/preferences.php');
    $node->add(s($text), $url);

    // User profile

    if (empty($CFG->messaging) and
        $PAGE->url->compare(new moodle_url('/user/view.php'), URL_MATCH_BASE)) {
        $userid = optional_param('id', false, PARAM_INT);
        if (local_mail_valid_recipient($userid)) {
            $vars = array('course' => $COURSE->id, 'recipient' => $userid);
            $PAGE->requires->string_for_js('sendmessage', 'local_mail');
            $PAGE->requires->js_init_code('M.local_mail = ' . json_encode($vars));
            $PAGE->requires->js('/local/mail/user.js');
        }
    }

    // Users list

    if (empty($CFG->messaging) and
        $PAGE->url->compare(new moodle_url('/user/index.php'), URL_MATCH_BASE)) {
        $userid = optional_param('id', false, PARAM_INT);
        $vars = array('course' => $COURSE->id);
        $PAGE->requires->string_for_js('choosedots', 'moodle');
        $PAGE->requires->strings_for_js(array(
                'bulkmessage',
                'to',
                'cc',
                'bcc',
                ), 'local_mail');
        $PAGE->requires->js_init_code('M.local_mail = ' . json_encode($vars));
        $PAGE->requires->js('/local/mail/users.js');
    }

    // Block progress

    if ($PAGE->url->compare(new moodle_url('/blocks/progress/overview.php'), URL_MATCH_BASE)) {
        $userid = optional_param('id', false, PARAM_INT);
        $vars = array('course' => $COURSE->id);
        $PAGE->requires->string_for_js('choosedots', 'moodle');
        $PAGE->requires->strings_for_js(array(
                'bulkmessage',
                'to',
                'cc',
                'bcc',
                ), 'local_mail');
        $PAGE->requires->js_init_code('M.local_mail = ' . json_encode($vars));
        $PAGE->requires->js('/local/mail/users.js');
    }
}
Ejemplo n.º 3
0
    public function toolbar($type, $courseid = 0, $params = null) {

        $replyall = isset($params['replyall']) ? $params['replyall'] : false;
        $paging = isset($params['paging']) ? $params['paging'] : null;
        $trash = isset($params['trash']) ? $params['trash'] : false;
        $labelid = isset($params['labelid']) ? $params['labelid'] : false;

        $toolbardown = false;
        if ($type === 'reply') {
            $viewcourse = array_key_exists($courseid, local_mail_get_my_courses());
            $output = $this->reply($viewcourse);
            // all recipients
            $output .= $this->replyall(($viewcourse and $replyall));
            $output .= $this->forward($viewcourse);
            $toolbardown = true;
        } else if ($type === 'forward') {
            $viewcourse = array_key_exists($courseid, local_mail_get_my_courses());
            $output = $this->forward($viewcourse);
            $toolbardown = true;
        } else {
            $selectall = $this->selectall();
            $labels = $extended = $goback = $search = $selectedlbl = '';
            if (!$trash and $type !== 'trash') {
                $labels = $this->labels($type);
            }
            $read = $unread = '';
            if ($type !== 'drafts') {
                $unread = $this->unread($type);
                $delete = $this->delete($trash);
            } else {
                $delete = $this->discard();
            }
            $pagingbar = '';
            if ($type !== 'view') {
                if ($type !== 'drafts') {
                    $read = $this->read();
                }
                $pagingbar = $this->paging($paging['offset'],
                                        $paging['count'],
                                        $paging['totalcount']);
                $search = $this->search();
            } else {
                $goback = $this->goback($paging);
            }
            if ($type === 'label') {
                $extended = $this->optlabels();
                $selectedlbl = $this->label(local_mail_label::fetch($labelid));
            }
            $more = $this->moreactions();
            $clearer = $this->output->container('', 'clearer');
            $left = html_writer::tag('div',
                $goback . $selectall . $labels . $read . $unread . $delete . $extended . $more . $search . $selectedlbl,
                array('class' => 'mail_buttons'));
            $output = $left . $pagingbar . $clearer;
        }
        return $this->output->container($output, ($toolbardown ? 'mail_toolbar_down' : 'mail_toolbar'));
    }
Ejemplo n.º 4
0
function local_mail_extends_navigation($root)
{
    global $CFG, $COURSE, $PAGE, $SESSION, $SITE, $USER, $DB;
    //Aqui
    $consulta = "SELECT count(*) as contador\n\t\t FROM " . $CFG->prefix . "course AS c JOIN " . $CFG->prefix . "context AS ctx ON c.id = ctx.instanceid JOIN " . $CFG->prefix . "role_assignments AS ra ON ra.contextid = ctx.id JOIN " . $CFG->prefix . "user AS u ON u.id = ra.userid WHERE (c.id = " . $COURSE->id . " AND u.id = " . $USER->id . " AND ra.roleid = 5) OR ( " . $USER->id . " = 2) OR (u.id =" . $USER->id . " AND ra.roleid = 5)";
    $bandera = 0;
    if ($courses = $DB->get_records_sql($consulta)) {
        foreach ($courses as $course) {
            if ($course->contador >= 1) {
                $bandera = 1;
            } else {
                $bandera = 0;
            }
        }
    }
    if ($bandera == 1) {
        if (!get_config('local_mail', 'version')) {
            return;
        }
        $courses = local_mail_get_my_courses();
        $count = local_mail_message::count_menu($USER->id);
        // My mail
        $text = get_string('mymail', 'local_mail');
        if (!empty($count->inbox)) {
            $text .= ' (' . $count->inbox . ')';
        }
        $node = navigation_node::create($text, null, navigation_node::TYPE_ROOTNODE);
        if (!empty($count->inbox)) {
            $node->add_class('local_mail_new_messages');
        }
        $child = $root->add_node($node, 'mycourses');
        $child->add_class('mail_root');
        // Compose
        $text = get_string('compose', 'local_mail');
        $url = new moodle_url('/local/mail/compose.php');
        $urlrecipients = new moodle_url('/local/mail/recipients.php');
        if ($PAGE->url->compare($url, URL_MATCH_BASE) or $PAGE->url->compare($urlrecipients, URL_MATCH_BASE)) {
            $url->param('m', $PAGE->url->param('m'));
        } else {
            $url = new moodle_url('/local/mail/create.php');
            if ($COURSE->id != $SITE->id) {
                $url->param('c', $COURSE->id);
                $url->param('sesskey', sesskey());
            }
        }
        $node->add(s($text), $url);
        // Inbox
        $text = get_string('inbox', 'local_mail');
        if (!empty($count->inbox)) {
            $text .= ' (' . $count->inbox . ')';
        }
        $url = new moodle_url('/local/mail/view.php', array('t' => 'inbox'));
        $child = $node->add(s($text), $url);
        $child->add_class('mail_inbox');
        // Starred
        $text = get_string('starredmail', 'local_mail');
        $url = new moodle_url('/local/mail/view.php', array('t' => 'starred'));
        $node->add(s($text), $url);
        // Drafts
        $text = get_string('drafts', 'local_mail');
        if (!empty($count->drafts)) {
            $text .= ' (' . $count->drafts . ')';
        }
        $url = new moodle_url('/local/mail/view.php', array('t' => 'drafts'));
        $child = $node->add(s($text), $url);
        $child->add_class('mail_drafts');
        // Sent
        $text = get_string('sentmail', 'local_mail');
        $url = new moodle_url('/local/mail/view.php', array('t' => 'sent'));
        $node->add(s($text), $url);
        // Courses
        if ($courses) {
            $text = get_string('courses', 'local_mail');
            $nodecourses = $node->add($text, null, navigation_node::TYPE_CONTAINER);
            foreach ($courses as $course) {
                $text = $course->shortname;
                if (!empty($count->courses[$course->id])) {
                    $text .= ' (' . $count->courses[$course->id] . ')';
                }
                $params = array('t' => 'course', 'c' => $course->id);
                $url = new moodle_url('/local/mail/view.php', $params);
                $child = $nodecourses->add(s($text), $url);
                $child->hidden = !$course->visible;
                $child->add_class('mail_course_' . $course->id);
            }
        }
        // Labels
        $labels = local_mail_label::fetch_user($USER->id);
        if ($labels) {
            $text = get_string('labels', 'local_mail');
            $nodelabels = $node->add($text, null, navigation_node::TYPE_CONTAINER);
            foreach ($labels as $label) {
                $text = $label->name();
                if (!empty($count->labels[$label->id()])) {
                    $text .= ' (' . $count->labels[$label->id()] . ')';
                }
                $params = array('t' => 'label', 'l' => $label->id());
                $url = new moodle_url('/local/mail/view.php', $params);
                $child = $nodelabels->add(s($text), $url);
                $child->add_class('mail_label_' . $label->id());
            }
        }
        // Trash
        $text = get_string('trash', 'local_mail');
        $url = new moodle_url('/local/mail/view.php', array('t' => 'trash'));
        $node->add(s($text), $url);
        //Aqui
    }
    // User profile
    if (empty($CFG->messaging) and $PAGE->url->compare(new moodle_url('/user/view.php'), URL_MATCH_BASE)) {
        $userid = optional_param('id', false, PARAM_INT);
        if (local_mail_valid_recipient($userid)) {
            $vars = array('course' => $COURSE->id, 'recipient' => $userid);
            $PAGE->requires->string_for_js('sendmessage', 'local_mail');
            $PAGE->requires->js_init_code('M.local_mail = ' . json_encode($vars));
            $PAGE->requires->js('/local/mail/user.js');
        }
    }
    // Users list
    if (empty($CFG->messaging) and $PAGE->url->compare(new moodle_url('/user/index.php'), URL_MATCH_BASE)) {
        $userid = optional_param('id', false, PARAM_INT);
        $vars = array('course' => $COURSE->id);
        $PAGE->requires->string_for_js('choosedots', 'moodle');
        $PAGE->requires->strings_for_js(array('bulkmessage', 'to', 'cc', 'bcc'), 'local_mail');
        $PAGE->requires->js_init_code('M.local_mail = ' . json_encode($vars));
        $PAGE->requires->js('/local/mail/users.js');
    }
}
Ejemplo n.º 5
0
 private static function from_records($record, $refrecords, $userrecords, $labelrecords)
 {
     $message = new self();
     $message->id = (int) $record->id;
     $message->course = (object) array('id' => $record->courseid, 'shortname' => $record->shortname, 'fullname' => $record->fullname, 'groupmode' => $record->groupmode);
     $message->subject = $record->subject;
     $message->content = $record->content;
     $message->format = (int) $record->format;
     $message->draft = (bool) $record->draft;
     $message->time = (int) $record->time;
     foreach ($refrecords as $r) {
         if ($r->messageid == $record->id) {
             $message->refs[] = $r->reference;
         }
     }
     foreach ($userrecords as $r) {
         if ($r->messageid == $record->id) {
             $message->role[$r->userid] = $r->role;
             $message->unread[$r->userid] = (bool) $r->unread;
             $message->starred[$r->userid] = (bool) $r->starred;
             $message->deleted[$r->userid] = (bool) $r->deleted;
             $fields = user_picture::fields('', array('username', 'maildisplay'));
             $userfields = array();
             foreach (explode(',', $fields) as $value) {
                 if ($value === 'id') {
                     continue;
                 }
                 $userfields[$value] = isset($r->{$value}) ? $r->{$value} : '';
             }
             $userfields['id'] = $r->userid;
             $message->users[$r->userid] = (object) $userfields;
         }
     }
     foreach ($labelrecords as $r) {
         if ($r->messageid == $record->id) {
             $message->labels[$r->id] = local_mail_label::from_record($r);
         }
     }
     return $message;
 }
Ejemplo n.º 6
0
function local_mail_js_labels() {
    global $USER;

    $labels = local_mail_label::fetch_user($USER->id);
    $js = 'M.local_mail = {mail_labels: {';
    $cont = 0;
    $total = count($labels);
    foreach ($labels as $label) {
        $js .= '"'.$label->id().'":{"id": "' . $label->id() . '", "name": "' . s($label->name()) . '", "color": "' . $label->color() . '"}';
        $cont++;
        if ($cont < $total) {
            $js .= ',';
        }
    }
    $js .= '}};';
    return $js;
}
    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);
    }
Ejemplo n.º 8
0
         $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()]) {
                 $message->add_label($label);
             } else {
                 $message->remove_label($label);
             }
         }
     }
 } else {
     if ($msgs) {
         $messages = local_mail_message::fetch_many($msgs);
         if (isset($data->labelid)) {
             $data->labelid = clean_param_array($data->labelid, PARAM_INT);
             $labels = local_mail_label::fetch_user($USER->id);
         }
         foreach ($messages as $message) {
             if (!$message->viewable($USER->id) or $message->deleted($USER->id)) {
                 print_error('invalidmessage', 'local_mail');
             }
             if (isset($data->labelid)) {
                 foreach ($labels as $label) {
                     if ($data->labelid[$label->id()]) {
                         $message->add_label($label);
                     } else {
                         $message->remove_label($label);
                     }
                 }
             }
         }
Ejemplo n.º 9
0
function local_mail_get_info()
{
    global $USER;
    $count = local_mail_message::count_menu($USER->id);
    $text = get_string('mymail', 'local_mail');
    if (empty($count->inbox)) {
        $count->root = $text;
    } else {
        $count->root = $text . ' (' . $count->inbox . ')';
    }
    $text = get_string('inbox', 'local_mail');
    if (empty($count->inbox)) {
        $count->inbox = $text;
    } else {
        $count->inbox = $text . ' (' . $count->inbox . ')';
    }
    $text = get_string('drafts', 'local_mail');
    if (empty($count->drafts)) {
        $count->drafts = $text;
    } else {
        $count->drafts = $text . ' (' . $count->drafts . ')';
    }
    $labels = local_mail_label::fetch_user($USER->id);
    if ($labels) {
        foreach ($labels as $label) {
            $text = $label->name();
            if (empty($count->labels[$label->id()])) {
                $count->labels[$label->id()] = $text;
            } else {
                $count->labels[$label->id()] = $text . ' (' . $count->labels[$label->id()] . ')';
            }
        }
    }
    if (!($courses = local_mail_get_my_courses())) {
        return;
    }
    foreach ($courses as $course) {
        $text = $course->shortname;
        if (empty($count->courses[$course->id])) {
            $count->courses[$course->id] = $text;
        } else {
            $count->courses[$course->id] = $text . ' (' . $count->courses[$course->id] . ')';
        }
    }
    return $count;
}