Beispiel #1
0
 /**
  * Search contacts.
  *
  * @param string $searchtext query string.
  * @param bool $onlymycourses limit the search to the user's courses only.
  * @return external_description
  * @since Moodle 2.5
  */
 public static function search_contacts($searchtext, $onlymycourses = false)
 {
     global $CFG, $USER, $PAGE;
     require_once $CFG->dirroot . '/user/lib.php';
     // Check if messaging is enabled.
     if (!$CFG->messaging) {
         throw new moodle_exception('disabled', 'message');
     }
     require_once $CFG->libdir . '/enrollib.php';
     $params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses);
     $params = self::validate_parameters(self::search_contacts_parameters(), $params);
     // Extra validation, we do not allow empty queries.
     if ($params['searchtext'] === '') {
         throw new moodle_exception('querystringcannotbeempty');
     }
     $courseids = array();
     if ($params['onlymycourses']) {
         $mycourses = enrol_get_my_courses(array('id'));
         foreach ($mycourses as $mycourse) {
             $courseids[] = $mycourse->id;
         }
     } else {
         $courseids[] = SITEID;
     }
     // Retrieving the users matching the query.
     $users = message_search_users($courseids, $params['searchtext']);
     $results = array();
     foreach ($users as $user) {
         $results[$user->id] = $user;
     }
     // Reorganising information.
     foreach ($results as &$user) {
         $newuser = array('id' => $user->id, 'fullname' => fullname($user));
         // Avoid undefined property notice as phone not specified.
         $user->phone1 = null;
         $user->phone2 = null;
         $userpicture = new user_picture($user);
         $userpicture->size = 1;
         // Size f1.
         $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
         $userpicture->size = 0;
         // Size f2.
         $newuser['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false);
         $user = $newuser;
     }
     return $results;
 }
Beispiel #2
0
/**
 * Print the results of a message search
 * @param mixed $frm submitted form data
 * @param bool $showicontext show text next to action icons?
 * @param object $user1 the current user
 */
function message_print_search_results($frm, $showicontext=false, $user1=null) {
    global $USER, $DB, $OUTPUT;

    if (empty($user1)) {
        $user1 = $USER;
    }

    echo html_writer::start_tag('div', array('class' => 'mdl-left'));

    $personsearch = false;
    $personsearchstring = null;
    if (!empty($frm->personsubmit) and !empty($frm->name)) {
        $personsearch = true;
        $personsearchstring = $frm->name;
    } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) {
        $personsearch = true;
        $personsearchstring = $frm->combinedsearch;
    }

    /// search for person
    if ($personsearch) {
        if (optional_param('mycourses', 0, PARAM_BOOL)) {
            $users = array();
            $mycourses = enrol_get_my_courses();
            foreach ($mycourses as $mycourse) {
                if (is_array($susers = message_search_users($mycourse->id, $personsearchstring))) {
                    foreach ($susers as $suser) $users[$suser->id] = $suser;
                }
            }
        } else {
            $users = message_search_users(SITEID, $personsearchstring);
        }

        if (!empty($users)) {
            echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
            echo get_string('userssearchresults', 'message', count($users));
            echo html_writer::end_tag('p');

            echo html_writer::start_tag('table', array('class' => 'messagesearchresults'));
            foreach ($users as $user) {

                if ( $user->contactlistid )  {
                    if ($user->blocked == 0) { /// not blocked
                        $strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext);
                        $strblock   = message_contact_link($user->id, 'block', true, null, $showicontext);
                    } else { // blocked
                        $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext);
                        $strblock   = message_contact_link($user->id, 'unblock', true, null, $showicontext);
                    }
                } else {
                    $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext);
                    $strblock   = message_contact_link($user->id, 'block', true, null, $showicontext);
                }

                //should we show just the icon or icon and text?
                $histicontext = 'icon';
                if ($showicontext) {
                    $histicontext = 'both';
                }
                $strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext);

                echo html_writer::start_tag('tr');

                echo html_writer::start_tag('td', array('class' => 'pix'));
                echo $OUTPUT->user_picture($user, array('size' => 20, 'courseid' => SITEID));
                echo html_writer::end_tag('td');

                echo html_writer::start_tag('td',array('class' => 'contact'));
                $action = null;
                $link = new moodle_url("/message/index.php?id=$user->id");
                echo $OUTPUT->action_link($link, fullname($user), $action, array('title' => get_string('sendmessageto', 'message', fullname($user))));
                echo html_writer::end_tag('td');

                echo html_writer::tag('td', $strcontact, array('class' => 'link'));
                echo html_writer::tag('td', $strblock, array('class' => 'link'));
                echo html_writer::tag('td', $strhistory, array('class' => 'link'));

                echo html_writer::end_tag('tr');
            }
            echo html_writer::end_tag('table');

        } else {
            echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
            echo get_string('userssearchresults', 'message', 0).'<br /><br />';
            echo html_writer::end_tag('p');
        }
    }

    // search messages for keywords
    $messagesearch = false;
    $messagesearchstring = null;
    if (!empty($frm->keywords)) {
        $messagesearch = true;
        $messagesearchstring = clean_text(trim($frm->keywords));
    } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) {
        $messagesearch = true;
        $messagesearchstring = clean_text(trim($frm->combinedsearch));
    }

    if ($messagesearch) {
        if ($messagesearchstring) {
            $keywords = explode(' ', $messagesearchstring);
        } else {
            $keywords = array();
        }
        $tome     = false;
        $fromme   = false;
        $courseid = 'none';

        if (empty($frm->keywordsoption)) {
            $frm->keywordsoption = 'allmine';
        }

        switch ($frm->keywordsoption) {
            case 'tome':
                $tome   = true;
                break;
            case 'fromme':
                $fromme = true;
                break;
            case 'allmine':
                $tome   = true;
                $fromme = true;
                break;
            case 'allusers':
                $courseid = SITEID;
                break;
            case 'courseusers':
                $courseid = $frm->courseid;
                break;
            default:
                $tome   = true;
                $fromme = true;
        }

        if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) {

        /// get a list of contacts
            if (($contacts = $DB->get_records('message_contacts', array('userid' => $USER->id), '', 'contactid, blocked') ) === false) {
                $contacts = array();
            }

        /// print heading with number of results
            echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
            $countresults = count($messages);
            if ($countresults == MESSAGE_SEARCH_MAX_RESULTS) {
                echo get_string('keywordssearchresultstoomany', 'message', $countresults).' ("'.s($messagesearchstring).'")';
            } else {
                echo get_string('keywordssearchresults', 'message', $countresults);
            }
            echo html_writer::end_tag('p');

        /// print table headings
            echo html_writer::start_tag('table', array('class' => 'messagesearchresults', 'cellspacing' => '0'));

            $headertdstart = html_writer::start_tag('td', array('class' => 'messagesearchresultscol'));
            $headertdend   = html_writer::end_tag('td');
            echo html_writer::start_tag('tr');
            echo $headertdstart.get_string('from').$headertdend;
            echo $headertdstart.get_string('to').$headertdend;
            echo $headertdstart.get_string('message', 'message').$headertdend;
            echo $headertdstart.get_string('timesent', 'message').$headertdend;
            echo html_writer::end_tag('tr');

            $blockedcount = 0;
            $dateformat = get_string('strftimedatetimeshort');
            $strcontext = get_string('context', 'message');
            foreach ($messages as $message) {

            /// ignore messages to and from blocked users unless $frm->includeblocked is set
                if (!optional_param('includeblocked', 0, PARAM_BOOL) and (
                      ( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or
                      ( isset($contacts[$message->useridto]  ) and ($contacts[$message->useridto]->blocked   == 1))
                                                )
                   ) {
                    $blockedcount ++;
                    continue;
                }

            /// load up user to record
                if ($message->useridto !== $USER->id) {
                    $userto = $DB->get_record('user', array('id' => $message->useridto));
                    $tocontact = (array_key_exists($message->useridto, $contacts) and
                                    ($contacts[$message->useridto]->blocked == 0) );
                    $toblocked = (array_key_exists($message->useridto, $contacts) and
                                    ($contacts[$message->useridto]->blocked == 1) );
                } else {
                    $userto = false;
                    $tocontact = false;
                    $toblocked = false;
                }

            /// load up user from record
                if ($message->useridfrom !== $USER->id) {
                    $userfrom = $DB->get_record('user', array('id' => $message->useridfrom));
                    $fromcontact = (array_key_exists($message->useridfrom, $contacts) and
                                    ($contacts[$message->useridfrom]->blocked == 0) );
                    $fromblocked = (array_key_exists($message->useridfrom, $contacts) and
                                    ($contacts[$message->useridfrom]->blocked == 1) );
                } else {
                    $userfrom = false;
                    $fromcontact = false;
                    $fromblocked = false;
                }

            /// find date string for this message
                $date = usergetdate($message->timecreated);
                $datestring = $date['year'].$date['mon'].$date['mday'];

            /// print out message row
                echo html_writer::start_tag('tr', array('valign' => 'top'));

                echo html_writer::start_tag('td', array('class' => 'contact'));
                message_print_user($userfrom, $fromcontact, $fromblocked, $showicontext);
                echo html_writer::end_tag('td');

                echo html_writer::start_tag('td', array('class' => 'contact'));
                message_print_user($userto, $tocontact, $toblocked, $showicontext);
                echo html_writer::end_tag('td');

                echo html_writer::start_tag('td', array('class' => 'summary'));
                echo message_get_fragment($message->fullmessage, $keywords);
                echo html_writer::start_tag('div', array('class' => 'link'));

                //find the user involved that isn't the current user
                $user2id = null;
                if ($user1->id == $message->useridto) {
                    $user2id = $message->useridfrom;
                } else {
                    $user2id = $message->useridto;
                }
                message_history_link($user1->id, $user2id, false,
                                     $messagesearchstring, 'm'.$message->id, $strcontext);
                echo html_writer::end_tag('div');
                echo html_writer::end_tag('td');

                echo html_writer::tag('td', userdate($message->timecreated, $dateformat), array('class' => 'date'));

                echo html_writer::end_tag('tr');
            }


            if ($blockedcount > 0) {
                echo html_writer::start_tag('tr');
                echo html_writer::tag('td', get_string('blockedmessages', 'message', $blockedcount), array('colspan' => 4, 'align' => 'center'));
                echo html_writer::end_tag('tr');
            }
            echo html_writer::end_tag('table');

        } else {
            echo html_writer::tag('p', get_string('keywordssearchresults', 'message', 0), array('class' => 'heading'));
        }
    }

    if (!$personsearch && !$messagesearch) {
        //they didn't enter any search terms
        echo $OUTPUT->notification(get_string('emptysearchstring', 'message'));
    }

    echo html_writer::end_tag('div');
}
Beispiel #3
0
function message_print_search_results($frm)
{
    global $USER, $CFG;
    echo '<div align="center">';
    /// search for person
    if (!empty($frm->personsubmit) and !empty($frm->name)) {
        if (optional_param('mycourses', 0, PARAM_BOOL)) {
            $users = array();
            $mycourses = get_my_courses($USER->id);
            foreach ($mycourses as $mycourse) {
                if (is_array($susers = message_search_users($mycourse->id, $frm->name))) {
                    foreach ($susers as $suser) {
                        $users[$suser->id] = $suser;
                    }
                }
            }
        } else {
            $users = message_search_users(SITEID, $frm->name);
        }
        if (!empty($users)) {
            echo '<strong>' . get_string('userssearchresults', 'message', count($users)) . '</strong>';
            echo '<table class="message_users">';
            foreach ($users as $user) {
                if ($user->contactlistid) {
                    if ($user->blocked == 0) {
                        /// not blocked
                        $strcontact = message_contact_link($user->id, 'remove', true);
                        $strblock = message_contact_link($user->id, 'block', true);
                    } else {
                        // blocked
                        $strcontact = message_contact_link($user->id, 'add', true);
                        $strblock = message_contact_link($user->id, 'unblock', true);
                    }
                } else {
                    $strcontact = message_contact_link($user->id, 'add', true);
                    $strblock = message_contact_link($user->id, 'block', true);
                }
                $strhistory = message_history_link($user->id, 0, true, '', '', 'icon');
                echo '<tr><td class="pix">';
                print_user_picture($user, SITEID, $user->picture, 20, false, true, 'userwindow');
                echo '</td>';
                echo '<td class="contact">';
                link_to_popup_window("/message/discussion.php?id={$user->id}", "message_{$user->id}", fullname($user), 500, 500, get_string('sendmessageto', 'message', fullname($user)), 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500');
                echo '</td>';
                echo '<td class="link">' . $strcontact . '</td>';
                echo '<td class="link">' . $strblock . '</td>';
                echo '<td class="link">' . $strhistory . '</td>';
                echo '</tr>';
            }
            echo '</table>';
        } else {
            notify(get_string('nosearchresults', 'message'));
        }
        /// search messages for keywords
    } else {
        if (!empty($frm->keywordssubmit) and !empty($frm->keywords)) {
            $keywordstring = clean_text(trim($frm->keywords));
            $keywords = explode(' ', $keywordstring);
            $tome = false;
            $fromme = false;
            $courseid = 'none';
            switch ($frm->keywordsoption) {
                case 'tome':
                    $tome = true;
                    break;
                case 'fromme':
                    $fromme = true;
                    break;
                case 'allmine':
                    $tome = true;
                    $fromme = true;
                    break;
                case 'allusers':
                    $courseid = SITEID;
                    break;
                case 'courseusers':
                    $courseid = $frm->courseid;
                    break;
                default:
                    $tome = true;
                    $fromme = true;
            }
            if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) {
                /// get a list of contacts
                if (($contacts = get_records('message_contacts', 'userid', $USER->id, '', 'contactid, blocked')) === false) {
                    $contacts = array();
                }
                /// print heading with number of results
                echo '<p class="heading">' . get_string('keywordssearchresults', 'message', count($messages)) . ' ("' . s($keywordstring) . '")</p>';
                /// print table headings
                echo '<table class="searchresults" cellspacing="0">';
                echo '<tr>';
                echo '<td><strong>' . get_string('from') . '</strong></td>';
                echo '<td><strong>' . get_string('to') . '</strong></td>';
                echo '<td><strong>' . get_string('message', 'message') . '</strong></td>';
                echo '<td><strong>' . get_string('timesent', 'message') . '</strong></td>';
                echo "</tr>\n";
                $blockedcount = 0;
                $dateformat = get_string('strftimedatetimeshort');
                $strcontext = get_string('context', 'message');
                foreach ($messages as $message) {
                    /// ignore messages to and from blocked users unless $frm->includeblocked is set
                    if (!optional_param('includeblocked', 0, PARAM_BOOL) and (isset($contacts[$message->useridfrom]) and $contacts[$message->useridfrom]->blocked == 1 or isset($contacts[$message->useridto]) and $contacts[$message->useridto]->blocked == 1)) {
                        $blockedcount++;
                        continue;
                    }
                    /// load up user to record
                    if ($message->useridto !== $USER->id) {
                        $userto = get_record('user', 'id', $message->useridto);
                        $tocontact = (array_key_exists($message->useridto, $contacts) and $contacts[$message->useridto]->blocked == 0);
                        $toblocked = (array_key_exists($message->useridto, $contacts) and $contacts[$message->useridto]->blocked == 1);
                    } else {
                        $userto = false;
                        $tocontact = false;
                        $toblocked = false;
                    }
                    /// load up user from record
                    if ($message->useridfrom !== $USER->id) {
                        $userfrom = get_record('user', 'id', $message->useridfrom);
                        $fromcontact = (array_key_exists($message->useridfrom, $contacts) and $contacts[$message->useridfrom]->blocked == 0);
                        $fromblocked = (array_key_exists($message->useridfrom, $contacts) and $contacts[$message->useridfrom]->blocked == 1);
                    } else {
                        $userfrom = false;
                        $fromcontact = false;
                        $fromblocked = false;
                    }
                    /// find date string for this message
                    $date = usergetdate($message->timecreated);
                    $datestring = $date['year'] . $date['mon'] . $date['mday'];
                    /// print out message row
                    echo '<tr valign="top">';
                    echo '<td class="contact">';
                    message_print_user($userfrom, $fromcontact, $fromblocked);
                    echo '</td>';
                    echo '<td class="contact">';
                    message_print_user($userto, $tocontact, $toblocked);
                    echo '</td>';
                    echo '<td class="summary">' . message_get_fragment($message->message, $keywords);
                    echo '<br /><div class="link">';
                    message_history_link($message->useridto, $message->useridfrom, false, $keywordstring, 'm' . $message->id, $strcontext);
                    echo '</div>';
                    echo '</td>';
                    echo '<td class="date">' . userdate($message->timecreated, $dateformat) . '</td>';
                    echo "</tr>\n";
                }
                if ($blockedcount > 0) {
                    echo '<tr><td colspan="4" align="center">' . get_string('blockedmessages', 'message', $blockedcount) . '</td></tr>';
                }
                echo '</table>';
            } else {
                notify(get_string('nosearchresults', 'message'));
            }
            /// what the ????, probably an empty search string, duh!
        } else {
            notify(get_string('emptysearchstring', 'message'));
        }
    }
    echo '<br />';
    print_single_button('index.php', array('tab' => 'search'), get_string('newsearch', 'message'));
    echo '</div>';
}
 /**
  * Test message_search_users.
  */
 public function test_message_search_users()
 {
     // Set this user as the admin.
     $this->setAdminUser();
     // Create a user to add to the admin's contact list.
     $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
     $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
     // Add users to the admin's contact list.
     message_add_contact($user1->id);
     message_add_contact($user2->id);
     // Add blocked contact.
     $this->assertCount(1, message_search_users(0, 'Test1'));
     $this->assertCount(2, message_search_users(0, 'Test'));
     $this->assertCount(1, message_search_users(0, 'user1'));
     $this->assertCount(2, message_search_users(0, 'user'));
 }
Beispiel #5
0
    /**
     * Search contacts.
     *
     * @param string $searchtext query string.
     * @param bool $onlymycourses limit the search to the user's courses only.
     * @return external_description
     * @since 2.5
     */
    public static function search_contacts($searchtext, $onlymycourses = false) {
        global $CFG, $USER;
        require_once($CFG->libdir . '/enrollib.php');

        $params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses);
        $params = self::validate_parameters(self::search_contacts_parameters(), $params);

        // Extra validation, we do not allow empty queries.
        if ($params['searchtext'] === '') {
            throw new moodle_exception('querystringcannotbeempty');
        }

        $courseids = array();
        if ($params['onlymycourses']) {
            $mycourses = enrol_get_my_courses(array('id'));
            foreach ($mycourses as $mycourse) {
                $courseids[] = $mycourse->id;
            }
        } else {
            $courseids[] = SITEID;
        }

        // Retrieving the users matching the query.
        $users = message_search_users($courseids, $params['searchtext']);
        $results = array();
        foreach ($users as $user) {
            $results[$user->id] = $user;
        }

        // Reorganising information.
        foreach ($results as &$user) {
            $newuser = array(
                'id' => $user->id,
                'fullname' => fullname($user)
            );

            // Avoid undefined property notice as phone not specified.
            $user->phone1 = null;
            $user->phone2 = null;

            // Try to get the user picture, but sometimes this method can return null.
            $userdetails = user_get_user_details($user, null, array('profileimageurl', 'profileimageurlsmall'));
            if (!empty($userdetails)) {
                $newuser['profileimageurl'] = $userdetails['profileimageurl'];
                $newuser['profileimageurlsmall'] = $userdetails['profileimageurlsmall'];
            }

            $user = $newuser;
        }

        return $results;
    }