Пример #1
0
        /// <summary> Sends one email to a user from the system including
        /// individual usage on items which are linked to the user </summary>
        /// <param name="UserID"> Primary key for whom to send the email </param>
        /// <param name="User_Name"> Name of the user to include in the email </param>
        /// <param name="User_Email"> Email address to use for this email </param>
        /// <param name="Year"> Year of statistics to highlight in the email </param>
        /// <param name="Month"> Month of statistics to highlight in the email </param>
        /// <param name="Number_Of_Items_To_Include"> Number of items to include in this email, in case the user has many, many items linked </param>
        /// <param name="System_URL"> Base URL to use for links to these items </param>
        /// <param name="System_Name"> Name of the SobekCM instance </param>
        /// <param name="FromAddress"> Address from which the email should be sent </param>
        /// <returns> TRUE if succesful, otherwise FALSE </returns>
        public static bool Send_Individual_Usage_Email(int UserID, string User_Name, string User_Email, int Year, int Month, int Number_Of_Items_To_Include, string System_URL, string System_Name, string FromAddress)
        {
            // If no email body was loaded, use the default
            if (String.IsNullOrEmpty(emailBody))
            {
                emailBody = DEFAULT_EMAIL_BODY;
            }

            try
            {
                // Get the item usage stats for this user on this month
                DataTable usageStats = Engine_Database.Get_User_Linked_Items_Stats(UserID, Month, Year, null);

                // Only continue if stats were returned
                if (usageStats != null)
                {
                    // Use the data view
                    DataView sortedView = new DataView(usageStats)
                    {
                        Sort = "Month_Hits DESC"
                    };

                    // Keep track for a total row at the bottom
                    int total_total_hits = 0;
                    int total_month_hits = 0;

                    // Build the string here
                    StringBuilder itemStatsBuilder = new StringBuilder();

                    // Display the stats for each item
                    int item_count = 0;
                    foreach (DataRowView thisRow in sortedView)
                    {
                        if (item_count < Number_Of_Items_To_Include)
                        {
                            string bibid = thisRow["BibID"].ToString();
                            string vid   = thisRow["VID"].ToString();

                            itemStatsBuilder.AppendLine("<strong><a href=\"" + System_URL + bibid + "/" + vid + "\">" + thisRow["Title"] + "</a></strong><br />");
                            itemStatsBuilder.AppendLine("<ul>");
                            itemStatsBuilder.AppendLine("  <li>Permanent Link: <a href=\"" + System_URL + bibid + "/" + vid + "\">" + System_URL + bibid + "/" + vid + "</a></li>");
                            itemStatsBuilder.AppendLine("  <li>Views");
                            itemStatsBuilder.AppendLine("    <ul>");
                            itemStatsBuilder.AppendLine("      <li>" + Month_From_Int(Month) + " " + Year + ": " + thisRow["Month_Hits"] + " views");
                            itemStatsBuilder.AppendLine("      <li>Total to date ( since " + Convert.ToDateTime(thisRow["CreateDate"]).ToShortDateString() + " ): " + thisRow["Total_Hits"] + " views");
                            itemStatsBuilder.AppendLine("    </ul>");
                            itemStatsBuilder.AppendLine("  </li>");
                            itemStatsBuilder.AppendLine("</ul>");
                        }

                        // Also, add the values
                        total_total_hits += Convert.ToInt32(thisRow["Total_Hits"]);
                        total_month_hits += Convert.ToInt32(thisRow["Month_Hits"]);
                        item_count++;
                    }

                    // Put some notes if there were more than 10 items
                    if (item_count > Number_Of_Items_To_Include)
                    {
                        itemStatsBuilder.Insert(0, TOO_MANY_ITEMS_MESSAGE.Replace("<%COUNT%>", item_count.ToString()));
                    }

                    string email_body_user = emailBody.Replace("<%TOTAL%>", Number_To_String(total_total_hits)).Replace("<%MONTHLY%>", Number_To_String(total_month_hits)).Replace("<%ITEMS%>", itemStatsBuilder.ToString()).Replace("<%DATE%>", Month_From_Int(Month) + " " + Year).Replace("<%NAME%>", User_Name).Replace("<%SYSURL%>", System_URL).Replace("<%SYSNAME%>", System_Name).Replace("<%YEAR%>", Year.ToString()).Replace("<%MONTH%>", Month.ToString().PadLeft(2, '0')) + "<br /><br /><p>( " + Month_From_Int(Month) + " " + Year + " )</p>";

                    // Only send the email if there was actually usage this month though
                    if (total_month_hits > 0)
                    {
                        // Send this email
                        EmailInfo newEmail = new EmailInfo
                        {
                            FromAddress    = FromAddress,
                            RecipientsList = User_Email,
                            isContactUs    = false,
                            isHTML         = true,
                            Subject        = EMAIL_SUBJECT.Replace("<%DATE%>", Month_From_Int(Month) + " " + Year),
                            Body           = email_body_user
                        };

                        string error;
                        return(Email_Helper.SendEmail(newEmail, out error));
                    }
                }

                // No actual error here, just turns out no items were linked to this user
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public override void Write_HTML(TextWriter Output, Custom_Tracer Tracer)
        {
            Tracer.Add_Trace("User_Usage_Stats_MySobekViewer.Write_HTML", String.Empty);

            string submode = RequestSpecificValues.Current_Mode.My_Sobek_SubMode;

            if (String.IsNullOrEmpty(submode))
            {
                submode = String.Empty;
            }

            // Determine the date (month,year) for the usage stats to display
            int month = UI_ApplicationCache_Gateway.Stats_Date_Range.Latest_Month;
            int year  = UI_ApplicationCache_Gateway.Stats_Date_Range.Latest_Year;

            if (submode.Length >= 6)
            {
                Int32.TryParse(submode.Substring(0, 4), out year);
                Int32.TryParse(submode.Substring(4, 2), out month);
            }

            char   sort_value = 'a';
            string sort_term  = "Title";

            if ((submode.Length == 1) || (submode.Length == 7))
            {
                switch (submode[submode.Length - 1])
                {
                case 'b':
                    sort_value = 'b';
                    sort_term  = "Total_Hits DESC";
                    break;

                case 'c':
                    sort_value = 'c';
                    sort_term  = "Total_Sessions DESC";
                    break;

                case 'd':
                    sort_value = 'd';
                    sort_term  = "Month_Hits DESC";
                    break;

                case 'e':
                    sort_value = '3';
                    sort_term  = "Month_Sessions DESC";
                    break;
                }
            }

            Output.WriteLine("<h1>" + Web_Title + "</h1>");
            Output.WriteLine();

            Output.WriteLine("<div class=\"SobekText\">");
            Output.WriteLine("<br />");
            Output.WriteLine("<p>Below is a list of items associated with your account including usage statistics.  Total views and visits represents the total amount of usage since the item was added to the library and the monthly views and visits is the usage in the selected month.  For more information about these terms, see the <a href=\"" + RequestSpecificValues.Current_Mode.Base_URL + "stats/usage/definitions\" target=\"_BLANK\">definitions on the main statistics page</a>.</p>");
            Output.WriteLine("<p>You may be the author, contributor, or associated with these items in some other way.</p>");
            Output.WriteLine("<p>To see statistics for a different month, change the selected month/year:");
            Output.WriteLine("<select name=\"date1_selector\" class=\"SobekStatsDateSelector\" onChange=\"window.location.href='" + RequestSpecificValues.Current_Mode.Base_URL + "my/stats/' + this.options[selectedIndex].value + '" + sort_value + "';\">");

            int select_month = UI_ApplicationCache_Gateway.Stats_Date_Range.Earliest_Month;
            int select_year  = UI_ApplicationCache_Gateway.Stats_Date_Range.Earliest_Year;

            while ((select_month != UI_ApplicationCache_Gateway.Stats_Date_Range.Latest_Month) || (select_year != UI_ApplicationCache_Gateway.Stats_Date_Range.Latest_Year))
            {
                if ((month == select_month) && (year == select_year))
                {
                    Output.WriteLine("      <option value=\"" + select_year + select_month.ToString().PadLeft(2, '0') + "\" selected=\"selected\" >" + Month_From_Int(select_month) + " " + select_year + "</option>");
                }
                else
                {
                    Output.WriteLine("      <option value=\"" + select_year + select_month.ToString().PadLeft(2, '0') + "\">" + Month_From_Int(select_month) + " " + select_year + "</option>");
                }

                select_month++;
                if (select_month > 12)
                {
                    select_month = 1;
                    select_year++;
                }
            }
            if ((month == select_month) && (year == select_year))
            {
                Output.WriteLine("      <option value=\"" + select_year + select_month.ToString().PadLeft(2, '0') + "\" selected=\"selected\" >" + Month_From_Int(select_month) + " " + select_year + "</option>");
            }
            else
            {
                Output.WriteLine("      <option value=\"" + select_year + select_month.ToString().PadLeft(2, '0') + "\">" + Month_From_Int(select_month) + " " + select_year + "</option>");
            }
            Output.WriteLine("    </select></p>");

            Output.WriteLine("<p>Select any column to re-sort this data.</p>");
            Output.WriteLine("<br />");

            // Get the item usage stats for this user on this month
            DataTable usageStats = Engine_Database.Get_User_Linked_Items_Stats(RequestSpecificValues.Current_User.UserID, month, year, Tracer);

            // Only continue if stats were returned
            if (usageStats != null)
            {
                // Use the data view
                DataView sortedView = new DataView(usageStats)
                {
                    Sort = sort_term
                };

                // Add the table header
                Output.WriteLine("<center>");
                Output.WriteLine("<table border=\"0px\" cellspacing=\"0px\" class=\"statsTable\">");
                Output.WriteLine("  <tr align=\"left\" bgcolor=\"#0022a7\" >");

                string redirect_value = RequestSpecificValues.Current_Mode.Base_URL + "my/stats/" + year + month.ToString().PadLeft(2, '0');

                if (sort_value != 'a')
                {
                    Output.WriteLine("    <th align=\"left\"><a href=\"" + redirect_value + "a\"><span style=\"color: White\">TITLE</span></a></th>");
                }
                else
                {
                    Output.WriteLine("    <th align=\"left\"><span style=\"color: White\">TITLE</span></th>");
                }

                if (sort_value != 'b')
                {
                    Output.WriteLine("    <th align=\"center\"><a href=\"" + redirect_value + "b\"><span style=\"color: White\">TOTAL VIEWS</span></a></th>");
                }
                else
                {
                    Output.WriteLine("    <th align=\"center\"><span style=\"color: White\">TOTAL VIEWS</span></th>");
                }

                if (sort_value != 'c')
                {
                    Output.WriteLine("    <th align=\"center\"><a href=\"" + redirect_value + "c\"><span style=\"color: White\">TOTAL VISITS</span></a></th>");
                }
                else
                {
                    Output.WriteLine("    <th align=\"center\"><span style=\"color: White\">TOTAL VISITS</span></th>");
                }

                if (sort_value != 'd')
                {
                    Output.WriteLine("    <th align=\"center\"><a href=\"" + redirect_value + "d\"><span style=\"color: White\">MONTHLY VIEWS</span></a></th>");
                }
                else
                {
                    Output.WriteLine("    <th align=\"center\"><span style=\"color: White\">MONTHLY VIEWS</span></th>");
                }

                if (sort_value != 'e')
                {
                    Output.WriteLine("    <th align=\"center\"><a href=\"" + redirect_value + "e\"><span style=\"color: White\">MONTHLY VISITS</span></a></th>");
                }
                else
                {
                    Output.WriteLine("    <th align=\"center\"><span style=\"color: White\">MONTHLY VISITS</span></th>");
                }


                Output.WriteLine("  </tr>");

                // Keep track for a total row at the bottom
                int total_total_hits     = 0;
                int total_total_sessions = 0;
                int total_month_hits     = 0;
                int total_month_sessions = 0;

                // Display the stats for each item
                foreach (DataRowView thisRow in sortedView)
                {
                    string bibid = thisRow["BibID"].ToString();
                    string vid   = thisRow["VID"].ToString();

                    Output.WriteLine("  <tr align=\"left\" >");
                    Output.WriteLine("    <td><a href=\"" + RequestSpecificValues.Current_Mode.Base_URL + bibid + "/" + vid + "/usage\" target=\"" + bibid + "_" + vid + "\">" + thisRow["Title"] + "</a></td>");
                    Output.WriteLine("    <td align=\"center\">" + thisRow["Total_Hits"] + "</td>");
                    Output.WriteLine("    <td align=\"center\">" + thisRow["Total_Sessions"] + "</td>");
                    Output.WriteLine("    <td align=\"center\">" + thisRow["Month_Hits"] + "</td>");
                    Output.WriteLine("    <td align=\"center\">" + thisRow["Month_Sessions"] + "</td>");
                    Output.WriteLine("  </tr>");
                    Output.WriteLine("  <tr><td bgcolor=\"#e7e7e7\" colspan=\"5\"></td></tr>");

                    // Also, add the values
                    total_total_hits     += Convert.ToInt32(thisRow["Total_Hits"]);
                    total_total_sessions += Convert.ToInt32(thisRow["Total_Sessions"]);
                    total_month_hits     += Convert.ToInt32(thisRow["Month_Hits"]);
                    total_month_sessions += Convert.ToInt32(thisRow["Month_Sessions"]);
                }

                Output.WriteLine("  <tr><td bgcolor=\"Black\" colspan=\"5\"></td></tr>");
                Output.WriteLine("  <tr align=\"left\" >");
                Output.WriteLine("    <td><strong>TOTAL</strong></td>");
                Output.WriteLine("    <td align=\"center\"><strong>" + total_total_hits + "</strong></td>");
                Output.WriteLine("    <td align=\"center\"><strong>" + total_total_sessions + "</strong></td>");
                Output.WriteLine("    <td align=\"center\"><strong>" + total_month_hits + "</strong></td>");
                Output.WriteLine("    <td align=\"center\"><strong>" + total_month_sessions + "</strong></td>");
                Output.WriteLine("  </tr>");



                Output.WriteLine("</table>");
                Output.WriteLine("</center>");
            }
            Output.WriteLine("<br /> <br />");
            Output.WriteLine("</div>");
        }