Skip to content

Commit

Permalink
Export group information
Browse files Browse the repository at this point in the history
  • Loading branch information
bostelm committed Jun 29, 2018
1 parent 2a06a92 commit 6f38790
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 12 deletions.
2 changes: 1 addition & 1 deletion export.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

$data = $mform->get_data();
if ($data) {
$availablefields = scheduler_get_export_fields();
$availablefields = scheduler_get_export_fields($scheduler);
$selectedfields = array();
foreach ($availablefields as $field) {
$inputid = 'field-'.$field->get_id();
Expand Down
2 changes: 1 addition & 1 deletion exportform.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function definition() {
private function add_exportfield_group($groupid, $labelid) {

$mform = $this->_form;
$fields = scheduler_get_export_fields();
$fields = scheduler_get_export_fields($this->scheduler);
$checkboxes = array();

foreach ($fields as $field) {
Expand Down
167 changes: 157 additions & 10 deletions exportlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public function get_header(scheduler_instance $scheduler) {
return get_string('field-'.$this->get_id(), 'scheduler');
}

/**
* Retrieve the header (in the sense of table header in the output) as an array.
* Needs to be overridden for multi-column fields only.
*
* @param $scheduler the scheduler instance in question
* @return string the header for this field
*/
public function get_headers(scheduler_instance $scheduler) {
return array($this->get_header($scheduler));
}

/**
* Retrieve the label used in the configuration form to label this field.
* By default, this equals the table header.
Expand All @@ -75,12 +86,22 @@ public function get_formlabel(scheduler_instance $scheduler) {
return $this->get_header($scheduler);
}

/**
* Retrieves the numer of table columns used by this field (typically 1).
*
* @param $scheduler the scheduler instance in question
* @return int the number of columns used
*/
public function get_num_columns(scheduler_instance $scheduler) {
return 1;
}

/**
* Retrieve the typical width (in characters) of this field.
* This is used to set the width of columns in the output, where this is relevant.
*
* @param $scheduler the scheduler instance in question
* @return int the width of this field (number of characters)
* @return int the width of this field (number of characters per column)
*/
public function get_typical_width(scheduler_instance $scheduler) {
return strlen($this->get_formlabel($scheduler));
Expand All @@ -104,6 +125,18 @@ public function is_wrapping() {
*/
public abstract function get_value(scheduler_slot $slot, $appointment);

/**
* Retrieve the value of this field as an array.
* Needs to be overriden for multi-column fields only.
*
* @param $slot the scheduler slot to get data from
* @param $appointment the appointment to evaluate (may be null for an empty slot)
* @return array an array of strings containing the column values
*/
public function get_values(scheduler_slot $slot, $appointment) {
return array($this->get_value($slot, $appointment));
}

}


Expand All @@ -112,7 +145,7 @@ public abstract function get_value(scheduler_slot $slot, $appointment);
*
* @return array the fields as an array of scheduler_export_field objects.
*/
function scheduler_get_export_fields() {
function scheduler_get_export_fields(scheduler_instance $scheduler) {
$result = array();
$result[] = new scheduler_slotdate_field();
$result[] = new scheduler_starttime_field();
Expand All @@ -135,6 +168,9 @@ function scheduler_get_export_fields() {
$result[] = new scheduler_profile_field('profile_'.$type, $id, $type);
}

$result[] = new scheduler_groups_single_field();
$result[] = new scheduler_groups_multi_field($scheduler);

$result[] = new scheduler_attended_field();
$result[] = new scheduler_grade_field();
$result[] = new scheduler_appointmentnote_field();
Expand Down Expand Up @@ -663,6 +699,109 @@ public function get_value(scheduler_slot $slot, $appointment) {

}

/**
* Export field: Student groups (in one column)
*
* @package mod_scheduler
* @copyright 2018 Henning Bostelmann and others (see README.txt)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scheduler_groups_single_field extends scheduler_export_field {

public function get_id() {
return 'groupssingle';
}

public function get_group() {
return 'student';
}

public function is_available(scheduler_instance $scheduler) {
$g = groups_get_all_groups($scheduler->courseid, 0, $scheduler->get_cm()->groupingid);
return count($g) > 0;
}

public function get_formlabel(scheduler_instance $scheduler) {
return get_string('field-groupssingle-label', 'scheduler');
}

public function get_value(scheduler_slot $slot, $appointment) {
if (! $appointment instanceof scheduler_appointment) {
return '';
}
$scheduler = $slot->get_scheduler();
$groups = groups_get_user_groups($scheduler->courseid, $appointment->studentid);
$groupingid = $scheduler->get_cm()->groupingid;
$gn = array();
foreach ($groups[$groupingid] as $groupid) {
$gn[] = groups_get_group_name($groupid);
}
return implode(',', $gn);
}

}

/**
* Export field: Student groups (in several columns)
*
* @package mod_scheduler
* @copyright 2018 Henning Bostelmann and others (see README.txt)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scheduler_groups_multi_field extends scheduler_export_field {

protected $coursegroups;

public function __construct(scheduler_instance $scheduler) {
$this->coursegroups = groups_get_all_groups($scheduler->courseid, 0, $scheduler->get_cm()->groupingid);
}

public function get_id() {
return 'groupsmulti';
}

public function get_group() {
return 'student';
}

public function is_available(scheduler_instance $scheduler) {
return count($this->coursegroups) > 0;
}

public function get_num_columns(scheduler_instance $scheduler) {
return count($this->coursegroups);
}

public function get_headers(scheduler_instance $scheduler) {
$result = array();
foreach ($this->coursegroups as $group) {
$result[] = $group->name;
}
return $result;
}

public function get_value(scheduler_slot $slot, $appointment) {
return '';
}

public function get_values(scheduler_slot $slot, $appointment) {
if (! $appointment instanceof scheduler_appointment) {
return '';
}
$usergroups = groups_get_user_groups($slot->get_scheduler()->courseid, $appointment->studentid)[0];
$result = array();
foreach ($this->coursegroups as $group) {
$key = in_array($group->id, $usergroups) ? 'yes' : 'no';
$result[] = get_string($key);
}
return $result;
}

}




/**
* An "output device" for scheduler exports
*
Expand Down Expand Up @@ -1313,10 +1452,13 @@ protected function build_page(scheduler_instance $scheduler, array $fields, arra
$col = 0;
foreach ($fields as $field) {
if ($field->get_group() != 'slot' || $mode != 'appointmentsgrouped') {
$header = $field->get_header($scheduler);
$this->canvas->write_string($row, $col, $header, $this->canvas->formatheader);
$this->canvas->set_column_width($col, $field->get_typical_width($scheduler));
$col++;
$headers = $field->get_headers($scheduler);
$numcols = $field->get_num_columns($scheduler);
for ($i = 0; $i < $numcols; $i++) {
$this->canvas->write_string($row, $col + $i, $headers[$i], $this->canvas->formatheader);
$this->canvas->set_column_width($col + $i, $field->get_typical_width($scheduler));
}
$col = $col + $numcols;
}
}
$row++;
Expand Down Expand Up @@ -1369,12 +1511,17 @@ protected function write_row($row, scheduler_slot $slot, $appointment, array $fi
if ($includeslotfields || $field->get_group() != 'slot') {
if ($multiple && $field->get_group() != 'slot') {
$value = get_string('multiple', 'scheduler');
$this->canvas->write_string($row, $col, $value);
$col++;
} else {
$value = $field->get_value($slot, $appointment);
$numcols = $field->get_num_columns($slot->get_scheduler());
$values = $field->get_values($slot, $appointment);
$format = $field->is_wrapping() ? $this->canvas->formatwrap : null;
for ($i = 0; $i < $numcols; $i++) {
$this->canvas->write_string($row, $col + $i, $values[$i], $format);
}
$col = $col + $numcols;
}
$format = $field->is_wrapping() ? $this->canvas->formatwrap : null;
$this->canvas->write_string($row, $col, $value, $format);
$col++;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions lang/en/scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@
$string['field-studentnote'] = 'Message by student';
$string['field-filecount'] = 'Number of uploaded files';
$string['field-grade'] = 'Grade';
$string['field-groupssingle'] = 'Groups';
$string['field-groupssingle-label'] = 'Groups (one column)';
$string['field-groupsmulti'] = 'Groups (several columns)';
$string['fileformat'] = 'File format';
$string['fileformat_help'] = 'The following file formats are available:
<ul>
Expand Down

0 comments on commit 6f38790

Please sign in to comment.