/**
 * This fuctions return all subfolders with one folder (one level), if it've
 *
 * @uses $USER, $COURSE
 * @param int $folderid Folder parent
 * @param int $courseid Course ID.
 * @param boolean $admin Admin folders
 * @return array Contain all subfolders
 * @todo Finish documenting this function
 **/
function email_get_subfolders($folderid, $courseid = NULL, $admin = false)
{
    global $USER;
    // Get childs for this parent
    $childs = get_records('email_subfolder', 'folderparentid', $folderid);
    $subfolders = array();
    // If have childs
    if ($childs) {
        // Save child folder in array
        foreach ($childs as $child) {
            if (is_null($courseid) or !email_have_asociated_folders($USER->id)) {
                $subfolders[] = get_record('email_folder', 'id', $child->folderchildid);
            } else {
                if ($folder = get_record('email_folder', 'id', $child->folderchildid, 'course', $courseid)) {
                    $subfolders[] = $folder;
                } else {
                    if ($folder = get_record('email_folder', 'id', $child->folderchildid, 'course', '0')) {
                        $subfolders[] = $folder;
                        // Add general folder's
                    }
                }
            }
        }
    } else {
        // If no childs, return false
        return false;
    }
    // Return subfolders
    return $subfolders;
}
 /**
  * When instance course has deleted, deleting all email_list course content.
  */
 function instance_delete()
 {
     /* When delete instance. Drop:
      * 		- Folders associated at this course.
      * 		- Mails of this course and all attachments.
      * 		- Colaterals effects:
      * 			# Drop all mail-folder references. (email_foldermail)
      * 			# Drop all sendbox/inbox (email_send)
      * 			# Drop all subfolders associated at this course.
      * 			# Drop all filters.
      */
     global $CFG, $DB;
     // Get course id
     if (!empty($this->instance)) {
         $courseid = $this->instance->pageid;
     } else {
         return true;
         // Skip
     }
     // Do not process SITEID
     if ($courseid === SITEID) {
         return true;
         // Skip
     }
     // Get all email references.
     if ($mailids = $DB->get_records('block_email_list_mail', array('course' => $courseid))) {
         foreach ($mailids as $mailid) {
             $DB->delete_records('block_email_list_foldermail', array('mailid' => $mailid));
         }
     }
     // Drop all mails
     $DB->delete_records('block_email_list_mail', array('course' => $courseid));
     if ($folders = $DB->get_records('block_email_list_folder', array('course' => $courseid))) {
         foreach ($folders as $folder) {
             // If user have defined who has associated folders-course, then delete all folders to this course.
             if (email_have_asociated_folders($folder->userid)) {
                 if (!email_removefolder($folder->id)) {
                     print_error('faildroprecordsfolder', 'block_email_list');
                 }
             }
         }
     }
     return true;
 }