Ejemplo n.º 1
0
// Search retrieves a set of FormSubmission's based on the search params
// provided in the query string.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formSubmissionHandle) Search(c *web.Context) error {
	formID := c.Params["form_id"]

	limit, err := strconv.Atoi(c.Request.URL.Query().Get("limit"))
	if err != nil {
		limit = 0
	}

	skip, err := strconv.Atoi(c.Request.URL.Query().Get("skip"))
	if err != nil {
		skip = 0
	}

	opts := submission.SearchOpts{
		Query:    c.Request.URL.Query().Get("search"),
		FilterBy: c.Request.URL.Query().Get("filterby"),
	}

	if c.Request.URL.Query().Get("orderby") == "dsc" {
		opts.DscOrder = true
	}

	results, err := submission.Search(c.SessionID, c.Ctx["DB"].(*db.DB), formID, limit, skip, opts)
	if err != nil {
		return err
	}

	c.Respond(results, http.StatusOK)

	return nil
}
Ejemplo n.º 2
0
// Download retrieves a set of FormSubmission's based on the search params
// provided in the query string and generates a CSV with the replies.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formSubmissionHandle) Download(c *web.Context) error {

	if c.Request.URL.Query().Get("download") != "true" {

		// Returns a URL To the CSV file.
		results := submission.SearchResults{}
		results.CSVURL = fmt.Sprintf("http://%v%v?download=true", c.Request.Host, c.Request.URL.Path)

		c.Respond(results, http.StatusOK)

		return nil
	}

	// Generates and returns the CSV file.

	// It will only arrive to this handler if the formID exists.
	formID := c.Params["form_id"]

	var (
		limit int
		skip  int
	)

	opts := submission.SearchOpts{
		Query:    c.Request.URL.Query().Get("search"),
		FilterBy: c.Request.URL.Query().Get("filterby"),
	}

	if c.Request.URL.Query().Get("orderby") == "dsc" {
		opts.DscOrder = true
	}

	results, err := submission.Search(c.SessionID, c.Ctx["DB"].(*db.DB), formID, limit, skip, opts)
	if err != nil {
		return err
	}

	// Convert into [][]string to encode the CSV.
	csvData, err := encodeSubmissionsToCSV(results.Submissions)
	if err != nil {
		return err
	}

	// Set the content type.
	c.Header().Set("Content-Type", "text/csv")
	c.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"ask_%s_%s.csv\"", formID, time.Now().String()))

	c.WriteHeader(http.StatusOK)
	c.Status = http.StatusOK

	c.Write(csvData)

	return nil
}
Ejemplo n.º 3
0
// SubmissionGroup retrieves submissions on a form by search criteria,
// groups them then returns text aggregations.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (aggregationHandle) SubmissionGroup(c *web.Context) error {
	id := c.Params["form_id"]

	// Unpack the search headers and create a SearchOpts for Group Submissions.
	limit, err := strconv.Atoi(c.Request.URL.Query().Get("limit"))
	if err != nil {
		limit = 0
	}

	skip, err := strconv.Atoi(c.Request.URL.Query().Get("skip"))
	if err != nil {
		skip = 0
	}

	opts := submission.SearchOpts{
		Query:    c.Request.URL.Query().Get("search"),
		FilterBy: c.Request.URL.Query().Get("filterby"),
	}

	if c.Request.URL.Query().Get("orderby") == "dsc" {
		opts.DscOrder = true
	}

	groups, err := form.GroupSubmissions(c.SessionID, c.Ctx["DB"].(*db.DB), id, limit, skip, opts)
	if err != nil {
		return err
	}

	groupKey, ok := c.Params["group_id"]
	if !ok {
		c.Respond(nil, http.StatusNotFound)
	}

	ta := []form.TextAggregation{}

	for group, submissions := range groups {

		if group.ID == groupKey {

			ta, err = form.TextAggregate(c.SessionID, c.Ctx["DB"].(*db.DB), id, submissions)
			if err != nil {
				return err
			}
		}
	}

	c.Respond(ta, http.StatusOK)

	return nil
}