Index: lib.php
===================================================================
RCS file: /cvsroot/moodle/contrib/plugins/blocks/email_list/email/lib.php,v
retrieving revision 1.10
diff -c -w -r1.10 lib.php
*** lib.php	22 Aug 2008 17:43:48 -0000	1.10
--- lib.php	28 Aug 2008 11:59:14 -0000
***************
*** 2771,2776 ****
--- 2771,2945 ----
  }
  
  /**
+  * This function returns an object of all users whithin current course who match
+  * the search query.
+  *  *Modified version of datalib.php's search_user() function
+  *
+  * @param object $course Current Course object
+  * @param string $query Search query
+  * @param boolean $dispadmins Flag to return course admins or not
+  * @param boolean $displayunconfirmed Flag to specify to return unconfirmed users
+  * @return object result set of all matching users
+  * @todo Add option to remove active user from results
+ */
+ function email_search_course_users($course, $query = '', $dispadmins = false, $dispunconfirmed = true) {
+     global $CFG, $USER;
+ 
+     $LIKE      = sql_ilike();
+ 
+     $order = 'ORDER BY firstname, lastname, id';
+ 
+     $select = 'u.deleted = \'0\'';
+     if (!$dispunconfirmed) {
+         $select .= ' AND u.confirmed = \'1\'';
+     }
+ 
+     if (!$course or $course->id == SITEID) {
+         $results = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
+                       FROM {$CFG->prefix}user u
+                       WHERE $select
+                           AND (u.firstname $LIKE '$query%' OR u.lastname $LIKE '$query%')
+                           AND u.username != 'guest'
+                           $order");
+     } else {
+         if ($course->id == SITEID) {
+             $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
+         } else {
+             $context = get_context_instance(CONTEXT_COURSE, $course->id);
+         }
+ 
+         $contextlists = get_related_contexts_string($context);
+ 
+         // Returns only group(s) members for users without the viewallgroups capability
+         $groupmembers = '';
+         // Separate groups
+         $groupmode = groups_get_course_groupmode($course);
+         if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
+             // Returns all groups current user is assigned to in course
+             if ($groups = groups_get_all_groups($course->id, $USER->id)) {
+                 $groupmembers = array();
+                 foreach ($groups as $group) {
+                     $groupmembers += groups_get_members($group->id, 'u.id');
+                 }
+                 if (!empty($groupmembers)) {
+                     $groupmembers = 'AND u.id IN ('.implode(',', array_keys($groupmembers)).')';
+                 } else {
+                     // Nobody in their groups :(
+                     return false;
+                 }
+             } else {
+                 // They have no group :(
+                 return false;
+             }
+         }
+ 
+         // Hides course admin roles (eg: admin && course creator) if requested (default)
+         if (!$dispadmins) {
+             $avoidroles = array();
+ 
+             if ($roles = get_roles_used_in_context($context, true)) {
+                 $canviewroles    = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
+                 $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $context);
+ 
+                 foreach ($roles as $role) {
+                     if (!isset($canviewroles[$role->id])) {   // Avoid this role (eg course creator)
+                         $avoidroles[] = $role->id;
+                         unset($roles[$role->id]);
+                         continue;
+                     }
+                     if (isset($doanythingroles[$role->id])) {   // Avoid this role (ie admin)
+                         $avoidroles[] = $role->id;
+                         unset($roles[$role->id]);
+                         continue;
+                     }
+                 }
+             }
+ 
+             // exclude users with roles we are avoiding
+             if ($avoidroles) {
+                 $adminroles = 'AND ra.roleid NOT IN (';
+                 $adminroles .= implode(',', $avoidroles);
+                 $adminroles .= ')';
+             } else {
+                 $adminroles = '';
+             }
+         } else {
+             $adminroles = '';
+         }
+ 
+         $results = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email
+                       FROM {$CFG->prefix}user u,
+                            {$CFG->prefix}role_assignments ra
+                       WHERE $select AND ra.contextid $contextlists AND ra.userid = u.id
+                           AND (u.firstname $LIKE '$query%' OR u.lastname $LIKE '$query%')
+                           AND (u.username != 'guest')
+                           $adminroles $groupmembers $order");
+     }
+ 
+     return $results;
+ }
+ 
+ /**
+  * This function validates a send to/cc/bcc field.  If the active user manually
+  * deletes a contact from the list, this may leave a hidden field with that users
+  * ID remaining;  This function takes care of that so the user will not be sent the mail.
+  *
+  * @param array $idarr array of the user id's
+  * @param string $namestr String of comma separated usernames
+  * @return boolean true if valid sendees, false if change was required
+ */
+ function email_validate_sendees(&$idarr, $namestr) {
+     global $CFG, $COURSE;
+ 
+     $namestrarr = email_clean_array(split(', ', $namestr));
+ 
+     if ($COURSE->id == SITEID) {
+             $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
+     } else {
+             $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
+     }
+     $contextlists = get_related_contexts_string($context);
+ 
+     $fnamesql  = sql_fullname('u.firstname', 'u.lastname');
+     $sql = 'SELECT u.id
+             FROM '.$CFG->prefix.'user u, '.$CFG->prefix.'role_assignments ra
+             WHERE ra.contextid '.$contextlists.'
+ 	    AND ra.userid = u.id AND '.$fnamesql.' = "';
+ 
+ 
+     // Checks the send to fields
+     $useridarr = array();
+     foreach ($namestrarr as $fullname) {
+         $user = get_records_sql($sql.$fullname.'"');
+         if (is_array($user) && !empty($user)) {
+             foreach ($user as $u) {
+                 $useridarr[] = $u->id;
+             }
+         }
+     }
+     // Combines those found with those existing to complete sendto list
+     // Value is passed by reference
+     $idarr = array_unique(array_merge($useridarr, array_intersect($useridarr, $idarr)));
+ 
+     return false;
+ }
+ 
+ /**
+  * Simple function to remove empty strings from the array
+  *
+  * @param array $array array of values
+  * @return array Array of values not empty
+ **/
+ function email_clean_array($array) {
+     foreach ($array as $index => $value) {
+         if (empty($value)) unset($array[$index]);
+     }
+     return $array;
+ }
+ 
+ 
+ 
+ /**
   * Prints the print emails button
   *
   * Idaho State University & MoodleRooms contrib - Thanks!
Index: mail_edit_form.php
===================================================================
RCS file: /cvsroot/moodle/contrib/plugins/blocks/email_list/email/mail_edit_form.php,v
retrieving revision 1.3
diff -c -w -r1.3 mail_edit_form.php
*** mail_edit_form.php	22 Aug 2008 17:08:31 -0000	1.3
--- mail_edit_form.php	28 Aug 2008 11:59:14 -0000
***************
*** 35,52 ****
          /// Print the required moodle fields first
          $mform->addElement('header', 'moodle', get_string('mail','email', NULL, EMAIL_LANG_PATH));
  
! 		$mform->addElement('button', 'urlcc', get_string('participants', 'email', NULL, EMAIL_LANG_PATH).'...' , array( 'onclick' => "this.target='participants'; return openpopup('/blocks/email_list/email/participants.php?id=$COURSE->id', 'participants', 'menubar=0,location=0,scrollbars=0,resizable,width=550,height=440', 0);" ) );
  
  		// Mail to
!         $mform->addElement('textarea', 'nameto', get_string('for', 'email', NULL, EMAIL_LANG_PATH), array('rows'=> '2', 'cols'=>'65', 'class'=>'textareacontacts', 'disabled'=>'true'));
  		//$mform->addRule('nameto', get_string('nosenders', 'email'), 'required', null, 'server');
  
          // Mail cc
!         $mform->addElement('textarea', 'namecc', get_string('cc', 'email', NULL, EMAIL_LANG_PATH), array('rows'=> '1', 'cols'=>'65', 'class'=>'textareacontacts', 'disabled'=>'true'));
  		//$mform->addRule('namecc', get_string('nosenders', 'email'), 'required', null, 'server');
  
  		// Mail bcc
! 		$mform->addElement('textarea', 'namebcc', get_string('bcc', 'email', NULL, EMAIL_LANG_PATH), array('rows'=> '1', 'cols'=>'65', 'class'=>'textareacontacts', 'disabled'=>'true'));
  		//$mform->addRule('namebcc', get_string('nosenders', 'email'), 'required', null, 'server');
  
          $mform->addElement('text','subject', get_string('subject', 'email', NULL, EMAIL_LANG_PATH),'class="emailsubject" maxlength="254" size="60"');
--- 35,79 ----
          /// Print the required moodle fields first
          $mform->addElement('header', 'moodle', get_string('mail','email', NULL, EMAIL_LANG_PATH));
  
! 		$mform->addElement('button', 'urlcc', get_string('participants', 'email', NULL, EMAIL_LANG_PATH).'...' , array( 'onclick' => "this.target='participants'; return openpopup('/blocks/email_list/email/participants.php?id=$COURSE->id', 'participants', 'menubar=0,location=0,scrollbars=1,resizable=1,width=500,height=540', 0);" ) );
  
  		// Mail to
! 
! 		// Added to allow for YUI autocomplete styling
! 		$mform->addElement('html','<div class="yui-skin-sam">');
! 
!         	$mform->addElement('textarea', 'nameto', get_string('for', 'email', NULL, EMAIL_LANG_PATH), array('rows'=> '2', 'cols'=>'65', 'class'=>'textareacontacts', 'multiple'=>'multiple'));
! 
! 		// Stores the YUI autocomplete results
! 		$mform->addElement('static', 'qResultsTo', '', '<div id="qResultsTo"></div>');
! 		$mform->addElement('html','</div>');
! 
  		//$mform->addRule('nameto', get_string('nosenders', 'email'), 'required', null, 'server');
  	
          	// Mail cc
! 
! 		// Added to allow for YUI autocomplete styling
! 		$mform->addElement('html','<div class="yui-skin-sam">');
! 			
! 		$mform->addElement('textarea', 'namecc', get_string('cc', 'email', NULL, EMAIL_LANG_PATH), array('rows'=> '2', 'cols'=>'65', 'class'=>'textareacontacts', 'multiple'=>'multiple'));
! 
! 		// Stores the YUI autocomplete results
! 		$mform->addElement('static', 'qResultsCC', '', '<div id="qResultsCC"></div>');
! 		$mform->addElement('html','</div>');
! 
  		//$mform->addRule('namecc', get_string('nosenders', 'email'), 'required', null, 'server');
  
  		// Mail bcc
! 
! 		// Added to allow for YUI autocomplete styling
! 		$mform->addElement('html','<div class="yui-skin-sam">');
! 
! 		$mform->addElement('textarea', 'namebcc', get_string('bcc', 'email', NULL, EMAIL_LANG_PATH), array('rows'=> '2', 'cols'=>'65', 'class'=>'textareacontacts', 'multiple'=>'multiple'));
! 
! 		// Stores the YUI autocomplete results
! 		$mform->addElement('static', 'qResultsBCC', '', '<div id="qResultsBCC"></div>');
! 		$mform->addElement('html','</div>');
! 
  		//$mform->addRule('namebcc', get_string('nosenders', 'email'), 'required', null, 'server');
  
          $mform->addElement('text','subject', get_string('subject', 'email', NULL, EMAIL_LANG_PATH),'class="emailsubject" maxlength="254" size="60"');
Index: manage.js
===================================================================
RCS file: /cvsroot/moodle/contrib/plugins/blocks/email_list/email/manage.js,v
retrieving revision 1.1
diff -c -w -r1.1 manage.js
*** manage.js	23 Jul 2008 04:01:47 -0000	1.1
--- manage.js	28 Aug 2008 11:59:14 -0000
***************
*** 72,78 ****
  
      // Adds Name to sendtype list
      if (field.value == '') {
!     	field.value = user;
      } else {
      	// Checks for valid string entry for post-send validation
          if ((field.value.charAt(field.value.length-2) != ',')) {
--- 72,78 ----
  
      // Adds Name to sendtype list
      if (field.value == '') {
!     	field.value = user+', ';
      } else {
      	// Checks for valid string entry for post-send validation
          if ((field.value.charAt(field.value.length-2) != ',')) {
***************
*** 83,89 ****
              }
          }
  
!         field.value = field.value + user;
  	}
      return true;
  }
--- 83,89 ----
              }
          }
  
!         field.value = field.value + user+', ';
  	}
      return true;
  }
***************
*** 248,254 ****
  
  //recarrega la cosa amb els par?metres
  function reloadiframe (params) {
!     var url = "search.php?id=<?php echo $id;?>"+params;
      document.getElementById("idsearch").src = url;
      // document.write( "Somthing" + url);
      //document.getElementById("search_res").innerHTML = url;
--- 248,254 ----
  
  //recarrega la cosa amb els par?metres
  function reloadiframe (params) {
!     var url = "get_users.php?"+params;
      document.getElementById("idsearch").src = url;
      // document.write( "Somthing" + url);
      //document.getElementById("search_res").innerHTML = url;
Index: sendmail.php
===================================================================
RCS file: /cvsroot/moodle/contrib/plugins/blocks/email_list/email/sendmail.php,v
retrieving revision 1.6
diff -c -w -r1.6 sendmail.php
*** sendmail.php	22 Aug 2008 17:43:48 -0000	1.6
--- sendmail.php	28 Aug 2008 11:59:14 -0000
***************
*** 147,156 ****
  		// Only redirect
  		redirect($CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid, '', '0');
  	} else if ( $form = $mailform->get_data() ) {
! 		if ( empty($form->to) and empty($form->cc) and empty($form->bcc) ) {
  			notify(get_string('nosenders', 'email', NULL, EMAIL_LANG_PATH));
  			$mailform->set_data($form);
  			$mailform->display();
  		} else 	if (! empty($form->send) or ! empty($form->draft)) {
  
  			// Create new eMail
--- 147,162 ----
  		// Only redirect
  		redirect($CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid, '', '0');
  	} else if ( $form = $mailform->get_data() ) {
! 		if ( empty($form->nameto) and empty($form->namecc) and empty($form->namebcc) ) {
  			notify(get_string('nosenders', 'email', NULL, EMAIL_LANG_PATH));
  			$mailform->set_data($form);
  			$mailform->display();
+ 			require_once($CFG->dirroot.'/blocks/email_list/email/contacts/yui_autocomplete.html');
+ 		} else if ( empty($form->to) and empty($form->cc) and empty($form->bcc) ) {
+ 			notify(get_string('invaliduser', 'email', NULL, EMAIL_LANG_PATH));
+ 			$mailform->set_data($form);
+ 			$mailform->display();
+ 			require_once($CFG->dirroot.'/blocks/email_list/email/contacts/yui_autocomplete.html');
  		} else 	if (! empty($form->send) or ! empty($form->draft)) {
  
  			// Create new eMail
***************
*** 195,206 ****
--- 201,216 ----
  
  			// Add users sent mail
  			if ( isset($form->to) ) {
+ 				// Make sure the to field names and ids match up
+ 				email_validate_sendees($form->to, $form->nameto);
  				$email->set_sendusersbyto($form->to);
  			}
  			if ( isset($form->cc) ) {
+ 				email_validate_sendees($form->cc, $form->namecc);
  				$email->set_sendusersbycc($form->cc);
  			}
  			if ( isset($form->bcc) ) {
+ 				email_validate_sendees($form->bcc, $form->namebcc);
  				$email->set_sendusersbybcc($form->bcc);
  			}
  
***************
*** 424,429 ****
--- 434,441 ----
  
      	$mailform->display();
  
+ 	require_once($CFG->dirroot.'/blocks/email_list/email/contacts/yui_autocomplete.html');
+ 
      	if ( $action == EMAIL_REPLY ) {
      	echo ' <script type="text/javascript" language="JavaScript"> var contacts = window.document.createElement("span");
  			        window.document.getElementById(\'id_nameto\').parentNode.appendChild(contacts);
