Skip to content

Commit

Permalink
Merge branch 'MDL-48679-master' of git://github.com/lameze/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Feb 10, 2015
2 parents 53018f5 + e82aa04 commit b300c1d
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 35 deletions.
2 changes: 2 additions & 0 deletions grade/export/grade_export_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ function definition() {
$submitstring = get_string('download');
if (empty($features['simpleui'])) {
$submitstring = get_string('submit');
} else if (!empty($CFG->gradepublishing)) {
$submitstring = get_string('export', 'grades');
}

$this->add_action_buttons(false, $submitstring);
Expand Down
179 changes: 169 additions & 10 deletions grade/export/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,30 @@ public function get_export_params() {
$itemidsparam = '-1';
}

$params = array('id' =>$this->course->id,
'groupid' =>$this->groupid,
'itemids' =>$itemidsparam,
'export_letters' =>$this->export_letters,
'export_feedback' =>$this->export_feedback,
'updatedgradesonly' =>$this->updatedgradesonly,
'displaytype' =>$this->displaytype,
'decimalpoints' =>$this->decimalpoints,
'export_onlyactive' =>$this->onlyactive,
'usercustomfields' =>$this->usercustomfields);
// We have a single grade display type constant.
if (!is_array($this->displaytype)) {
$displaytypes = $this->displaytype;
} else {
// Implode the grade display types array as moodle_url function doesn't accept arrays.
$displaytypes = implode(',', $this->displaytype);
}

if (!empty($this->updatedgradesonly)) {
$updatedgradesonly = $this->updatedgradesonly;
} else {
$updatedgradesonly = 0;
}
$params = array('id' => $this->course->id,
'groupid' => $this->groupid,
'itemids' => $itemidsparam,
'export_letters' => $this->export_letters,
'export_feedback' => $this->export_feedback,
'updatedgradesonly' => $updatedgradesonly,
'decimalpoints' => $this->decimalpoints,
'export_onlyactive' => $this->onlyactive,
'usercustomfields' => $this->usercustomfields,
'displaytype' => $displaytypes,
'key' => $this->userkey);

return $params;
}
Expand Down Expand Up @@ -436,6 +450,151 @@ public function print_continue() {

return;
}

/**
* Generate the export url.
*
* Get submitted form data and create the url to be used on the grade publish feature.
*
* @return moodle_url the url of grade publishing export.
*/
public function get_export_url() {
return new moodle_url('/grade/export/'.$this->plugin.'/dump.php', $this->get_export_params());
}

/**
* Convert the grade display types parameter into the required array to grade exporting class.
*
* In order to export, the array key must be the display type name and the value must be the grade display type
* constant.
*
* Note: Added support for combined display types constants like the (GRADE_DISPLAY_TYPE_PERCENTAGE_REAL) as
* the $CFG->grade_export_displaytype config is still used on 2.7 in case of missing displaytype url param.
* In these cases, the file will be exported with a column for each display type.
*
* @param string $displaytypes can be a single or multiple display type constants comma separated.
* @return array $types
*/
public static function convert_flat_displaytypes_to_array($displaytypes) {
$types = array();

// We have a single grade display type constant.
if (is_int($displaytypes)) {
$displaytype = clean_param($displaytypes, PARAM_INT);

// Let's set a default value, will be replaced below by the grade display type constant.
$display[$displaytype] = 1;
} else {
// Multiple grade display types constants.
$display = array_flip(explode(',', $displaytypes));
}

// Now, create the array in the required format by grade exporting class.
foreach ($display as $type => $value) {
$type = clean_param($type, PARAM_INT);
if ($type == GRADE_DISPLAY_TYPE_LETTER) {
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
} else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
} else if ($type == GRADE_DISPLAY_TYPE_REAL) {
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
} else if ($type == GRADE_DISPLAY_TYPE_REAL_PERCENTAGE) {
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
} else if ($type == GRADE_DISPLAY_TYPE_REAL_LETTER) {
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
} else if ($type == GRADE_DISPLAY_TYPE_LETTER_REAL) {
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
} else if ($type == GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE) {
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
} else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER) {
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
$types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
} else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE_REAL) {
$types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
$types['real'] = GRADE_DISPLAY_TYPE_REAL;
}
}
return $types;
}

/**
* Convert the item ids parameter into the required array to grade exporting class.
*
* In order to export, the array key must be the grade item id and all values must be one.
*
* @param string $itemids can be a single item id or many item ids comma separated.
* @return array $items correctly formatted array.
*/
public static function convert_flat_itemids_to_array($itemids) {
$items = array();

// We just have one single item id.
if (is_int($itemids)) {
$itemid = clean_param($itemids, PARAM_INT);
$items[$itemid] = 1;
} else {
// Few grade items.
$items = array_flip(explode(',', $itemids));
foreach ($items as $itemid => $value) {
$itemid = clean_param($itemid, PARAM_INT);
$items[$itemid] = 1;
}
}
return $items;
}

/**
* Create the html code of the grade publishing feature.
*
* @return string $output html code of the grade publishing.
*/
public function get_grade_publishing_url() {
$url = $this->get_export_url();
$output = html_writer::start_div();
$output .= html_writer::tag('p', get_string('gradepublishinglink', 'grades', html_writer::link($url, $url)));
$output .= html_writer::end_div();
return $output;
}

/**
* Create a stdClass object from URL parameters to be used by grade_export class.
*
* @param int $id course id.
* @param string $itemids grade items comma separated.
* @param bool $exportfeedback export feedback option.
* @param bool $onlyactive only enrolled active students.
* @param string $displaytype grade display type constants comma separated.
* @param int $decimalpoints grade decimal points.
* @param null $updatedgradesonly recently updated grades only (Used by XML exporting only).
* @param null $separator separator character: tab, comma, colon and semicolon (Used by TXT exporting only).
*
* @return stdClass $formdata
*/
public static function export_bulk_export_data($id, $itemids, $exportfeedback, $onlyactive, $displaytype,
$decimalpoints, $updatedgradesonly = null, $separator = null) {

$formdata = new \stdClass();
$formdata->id = $id;
$formdata->itemids = self::convert_flat_itemids_to_array($itemids);
$formdata->exportfeedback = $exportfeedback;
$formdata->export_onlyactive = $onlyactive;
$formdata->display = self::convert_flat_displaytypes_to_array($displaytype);
$formdata->decimals = $decimalpoints;

if (!empty($updatedgradesonly)) {
$formdata->updatedgradesonly = $updatedgradesonly;
}

if (!empty($separator)) {
$formdata->separator = $separator;
}

return $formdata;
}
}

/**
Expand Down
24 changes: 21 additions & 3 deletions grade/export/ods/dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@

define('NO_MOODLE_COOKIES', true); // session not used here
require_once '../../../config.php';
require_once($CFG->dirroot.'/grade/export/ods/grade_export_ods.php');

$id = required_param('id', PARAM_INT);
$groupid = optional_param('groupid', 0, PARAM_INT);
$itemids = required_param('itemids', PARAM_RAW);
$exportfeedback = optional_param('export_feedback', 0, PARAM_BOOL);
$displaytype = optional_param('displaytype', $CFG->grade_export_displaytype, PARAM_RAW);
$decimalpoints = optional_param('decimalpoints', $CFG->grade_export_decimalpoints, PARAM_INT);
$onlyactive = optional_param('export_onlyactive', 0, PARAM_BOOL);

$id = required_param('id', PARAM_INT); // course id
if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('nocourseid');
}
Expand All @@ -30,9 +38,19 @@
}

$context = context_course::instance($id);
require_capability('moodle/grade:export', $context);
require_capability('gradeexport/ods:view', $context);
require_capability('gradeexport/ods:publish', $context);

// use the same page parameters as export.php and append &key=sdhakjsahdksahdkjsahksadjksahdkjsadhksa
require 'export.php';
if (!groups_group_visible($groupid, $COURSE)) {
print_error('cannotaccessgroup', 'grades');
}

// Get all url parameters and create an object to simulate a form submission.
$formdata = grade_export::export_bulk_export_data($id, $itemids, $exportfeedback, $onlyactive, $displaytype,
$decimalpoints);

$export = new grade_export_ods($course, $groupid, $formdata);
$export->print_grades();


21 changes: 18 additions & 3 deletions grade/export/ods/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_once 'grade_export_ods.php';

$id = required_param('id', PARAM_INT); // course id
$PAGE->set_url('/grade/export/ods/export.php', array('id'=>$id));

if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('nocourseid');
Expand All @@ -32,16 +33,30 @@
require_capability('moodle/grade:export', $context);
require_capability('gradeexport/ods:view', $context);

// We need to call this method here before any output otherwise the menu won't display.
// If you use this method without this check, will break the direct grade exporting (without publishing).
$key = optional_param('key', '', PARAM_RAW);
if (!empty($CFG->gradepublishing) && !empty($key)) {
print_grade_page_head($COURSE->id, 'export', 'ods', get_string('exportto', 'grades') . ' ' . get_string('pluginname', 'gradeexport_ods'));
}

if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
if (!groups_is_member($groupid, $USER->id)) {
print_error('cannotaccessgroup', 'grades');
}
}
$mform = new grade_export_form(null, array('publishing' => true, 'simpleui' => true, 'multipledisplaytypes' => true));
$data = $mform->get_data();

// print all the exported data here
$export = new grade_export_ods($course, $groupid, $data);
$export->print_grades();

// If the gradepublishing is enabled and user key is selected print the grade publishing link.
if (!empty($CFG->gradepublishing) && !empty($key)) {
groups_print_course_menu($course, 'index.php?id='.$id);
echo $export->get_grade_publishing_url();
echo $OUTPUT->footer();
} else {
$export->print_grades();
}



25 changes: 22 additions & 3 deletions grade/export/txt/dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@

define('NO_MOODLE_COOKIES', true); // session not used here
require_once '../../../config.php';
require_once($CFG->dirroot.'/grade/export/txt/grade_export_txt.php');

$id = required_param('id', PARAM_INT);
$groupid = optional_param('groupid', 0, PARAM_INT);
$itemids = required_param('itemids', PARAM_RAW);
$exportfeedback = optional_param('export_feedback', 0, PARAM_BOOL);
$separator = optional_param('separator', 'comma', PARAM_ALPHA);
$displaytype = optional_param('displaytype', $CFG->grade_export_displaytype, PARAM_RAW);
$decimalpoints = optional_param('decimalpoints', $CFG->grade_export_decimalpoints, PARAM_INT);
$onlyactive = optional_param('export_onlyactive', 0, PARAM_BOOL);

$id = required_param('id', PARAM_INT); // course id
if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('nocourseid');
}
Expand All @@ -30,9 +39,19 @@
}

$context = context_course::instance($id);
require_capability('moodle/grade:export', $context);
require_capability('gradeexport/txt:publish', $context);
require_capability('gradeexport/txt:view', $context);

if (!groups_group_visible($groupid, $COURSE)) {
print_error('cannotaccessgroup', 'grades');
}

// Get all url parameters and create an object to simulate a form submission.
$formdata = grade_export::export_bulk_export_data($id, $itemids, $exportfeedback, $onlyactive, $displaytype,
$decimalpoints, null, $separator);

// use the same page parameters as export.php and append &key=sdhakjsahdksahdkjsahksadjksahdkjsadhksa
require 'export.php';
$export = new grade_export_txt($course, $groupid, $formdata);
$export->print_grades();


21 changes: 18 additions & 3 deletions grade/export/txt/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_once 'grade_export_txt.php';

$id = required_param('id', PARAM_INT); // course id
$PAGE->set_url('/grade/export/txt/export.php', array('id'=>$id));

if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('nocourseid');
Expand All @@ -32,6 +33,13 @@
require_capability('moodle/grade:export', $context);
require_capability('gradeexport/txt:view', $context);

// We need to call this method here before any print otherwise the menu won't display.
// If you use this method without this check, will break the direct grade exporting (without publishing).
$key = optional_param('key', '', PARAM_RAW);
if (!empty($CFG->gradepublishing) && !empty($key)) {
print_grade_page_head($COURSE->id, 'export', 'txt', get_string('exportto', 'grades') . ' ' . get_string('pluginname', 'gradeexport_txt'));
}

if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
if (!groups_is_member($groupid, $USER->id)) {
print_error('cannotaccessgroup', 'grades');
Expand All @@ -46,8 +54,15 @@
);
$mform = new grade_export_form(null, $params);
$data = $mform->get_data();

// Print all the exported data here.
$export = new grade_export_txt($course, $groupid, $data);
$export->print_grades();

// If the gradepublishing is enabled and user key is selected print the grade publishing link.
if (!empty($CFG->gradepublishing) && !empty($key)) {
groups_print_course_menu($course, 'index.php?id='.$id);
echo $export->get_grade_publishing_url();
echo $OUTPUT->footer();
} else {
$export->print_grades();
}


Loading

0 comments on commit b300c1d

Please sign in to comment.