Skip to content

Commit

Permalink
MDL-11034 Implemented the global enablepublishing and refactored the …
Browse files Browse the repository at this point in the history
…handling of userkey for each export plugin. Also added a dump.php file in each plugin directory, although this could easily be refactored into 1 file in the parent folder, pointing to each plugin's export.php file for output differences.
  • Loading branch information
nicolasconnault committed Aug 31, 2007
1 parent 9133f93 commit 0e2d708
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 101 deletions.
73 changes: 63 additions & 10 deletions grade/export/lib.php
Expand Up @@ -40,8 +40,9 @@ class grade_export {
var $columnidnumbers = array(); // Collect all gradeitem id numbers
var $students = array();
var $course; // course
var $publish; // Whether to publish this data via URL, or dump it to browser as usual
var $userkey; // Optional MD5 string used to publish this export data via a URL
var $export_letters;
var $itemidsurl; // A string of itemids to add to the URL for the export

// common strings
var $strgrades;
Expand All @@ -51,15 +52,25 @@ class grade_export {
* Constructor should set up all the private variables ready to be pulled
* @param int $courseid course id
* @param array $itemids array of grade item ids, empty means all
* @param boolean $export_letters Whether to export letter grade_items as literal letters, or as numerical values
* @param boolean $publish published using private user key
* @param stdClass $formdata Optional object of formdata.
* @note Exporting as letters will lead to data loss if that exported set it re-imported.
*/
function grade_export($courseid, $itemids=null, $export_letters=false, $publish=false) {
global $CFG;
function grade_export($courseid, $itemids=null, $formdata=null) {
global $CFG, $USER, $COURSE;

$this->export_letters = false;
if (isset($formdata->export_letters)) {
$this->export_letters = $formdata->export_letters;
}

$this->userkey = false;
if (isset($formdata->key)) {
if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) { // Create a new key
$formdata->key = create_user_key('grade/export', $USER->id, $COURSE->id, $formdata->iprestriction, $formdata->validuntil);
}
$this->userkey = $formdata->key;
}

$this->publish = $publish;
$this->export_letters = $export_letters;
$this->strgrades = get_string("grades");
$this->strgrade = get_string("grade");

Expand Down Expand Up @@ -121,9 +132,27 @@ function grade_export($courseid, $itemids=null, $export_letters=false, $publish=
$this->comments[$student->id] = array(); // Collect all comments in tihs array
}
}

if (isset($formdata->itemids)) {
// Build itemidsurl for links
$itemids = array();
if ($formdata->itemids) {
foreach ($formdata->itemids as $itemid=>$selected) {
if ($selected) {
$itemids[] = $itemid;
}
}
$this->itemidsurl = implode(",", $itemids);
} else {
//error?
$this->itemidsurl = '';
}
}
}

function load_grades() {
global $CFG;

// first make sure we have all final grades
// TODO: check that no grade_item has needsupdate set
grade_regrade_final_grades($this->id);
Expand Down Expand Up @@ -155,8 +184,7 @@ function load_grades() {
$grade_item_displaytype = $report->get_pref('gradedisplaytype', $gradeitem->id);
// TODO Convert final grade to letter if export option is on, and grade_item is set to letter type MDL-10490
if ($grade_item_displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER) {
$finalgrade = grade_grade::get_letter($letters, $finalgrade,
$gradeitem->grademin, $gradeitem->grademax);
$finalgrade = grade_grade::get_letter($letters, $finalgrade, $gradeitem->grademin, $gradeitem->grademax);
}
}

Expand All @@ -169,7 +197,7 @@ function load_grades() {
}

/**
* To be implemented by child classe
* To be implemented by child class
* TODO finish PHPdocs
*/
function print_grades() { }
Expand Down Expand Up @@ -225,6 +253,31 @@ function display_grades($feedback=false, $rows=10) {
}
echo '</table>';
}

/**
* Either prints a "continue" box, which will redirect the user to the download page, or prints the URL for the published data.
* @note exit() at the end of the method
* @param string $plugin Required: name of the plugin calling this method. Used for building the URL.
* @return void
*/
function print_continue($plugin) {
global $CFG;

// this redirect should trigger a download prompt
if (!$this->userkey) {
print_continue('export.php?id='.$this->id.'&amp;itemids='.$this->itemidsurl.'&amp;export_letters='.$this->export_letters);

} else {
$link = $CFG->wwwroot.'/grade/export/'.$plugin.'/dump.php?id='.$this->id.'&amp;itemids='
. $this->itemidsurl.'&amp;export_letters='.$this->export_letters.'&amp;key='.$this->userkey;

echo '<p>';
echo '<a href="'.$link.'">'.$link.'</a>';
echo '</p>';
print_footer();
}
exit();
}
}

?>
13 changes: 13 additions & 0 deletions grade/export/ods/dump.php
@@ -0,0 +1,13 @@
<?php //$Id$

$nomoodlecookie = true; // session not used here
require '../../../config.php';

$id = required_param('id', PARAM_INT); // course id

require_user_key_login('grade/export', $id); // we want different keys for each course

// use the same page parameters as export.php and append &key=sdhakjsahdksahdkjsahksadjksahdkjsadhksa
require 'export.php';

?>
23 changes: 4 additions & 19 deletions grade/export/ods/index.php
Expand Up @@ -48,29 +48,14 @@
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
print_grade_plugin_selector($id, 'export', 'ods');

$mform = new grade_export_form(null, array('publishing' => true));
$mform = new grade_export_form(null, array('publishing' => $CFG->enablepublishing));

// process post information
if ($data = $mform->get_data()) {
$itemids = array();
if ($data->itemids) {
foreach ($data->itemids as $itemid=>$selected) {
if ($selected) {
$itemids[] = $itemid;
}
}
$itemidsurl = implode(",", $itemids);
} else {
//error?
$itemidsurl = '';
}

$export = new grade_export($id, $itemids, $data->export_letters);
// print the grades on screen for feedbacks
$export = new grade_export($id, $itemids, $data);
$export->display_grades($feedback, $data->previewrows);

// this redirect should trigger a download prompt
redirect('export.php?id='.$id.'&amp;itemids='.$itemidsurl.'&amp;export_letters='.$data->export_letters);
exit;
$export->print_continue('ods');
}

$mform->display();
Expand Down
13 changes: 13 additions & 0 deletions grade/export/txt/dump.php
@@ -0,0 +1,13 @@
<?php //$Id$

$nomoodlecookie = true; // session not used here
require '../../../config.php';

$id = required_param('id', PARAM_INT); // course id

require_user_key_login('grade/export', $id); // we want different keys for each course

// use the same page parameters as export.php and append &key=sdhakjsahdksahdkjsahksadjksahdkjsadhksa
require 'export.php';

?>
23 changes: 4 additions & 19 deletions grade/export/txt/index.php
Expand Up @@ -48,29 +48,14 @@
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
print_grade_plugin_selector($id, 'export', 'txt');

$mform = new grade_export_form(null, array('includeseparator'=>true, 'publishing' => true));
$mform = new grade_export_form(null, array('includeseparator'=>true, 'publishing' => $CFG->enablepublishing));

// process post information
if ($data = $mform->get_data()) {
$itemids = array();
if ($data->itemids) {
foreach ($data->itemids as $itemid=>$selected) {
if ($selected) {
$itemids[] = $itemid;
}
}
$itemidsurl = implode(",", $itemids);
} else {
//error?
$itemidsurl = '';
}

$export = new grade_export($id, $itemids, $data->export_letters);
// print the grades on screen for feedbacks
$export = new grade_export($id, $itemids, $data);
$export->display_grades($feedback, $data->previewrows);

// this redirect should trigger a download prompt
redirect('export.php?id='.$id.'&amp;itemids='.$itemidsurl.'&amp;separator='.$data->separator.'&amp;export_letters='.$data->export_letters);
exit;
$export->print_continue('txt');
}

// print the form to choose what grade_items to export
Expand Down
13 changes: 13 additions & 0 deletions grade/export/xls/dump.php
@@ -0,0 +1,13 @@
<?php //$Id$

$nomoodlecookie = true; // session not used here
require '../../../config.php';

$id = required_param('id', PARAM_INT); // course id

require_user_key_login('grade/export', $id); // we want different keys for each course

// use the same page parameters as export.php and append &key=sdhakjsahdksahdkjsahksadjksahdkjsadhksa
require 'export.php';

?>
23 changes: 4 additions & 19 deletions grade/export/xls/index.php
Expand Up @@ -48,29 +48,14 @@
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
print_grade_plugin_selector($id, 'export', 'xls');

$mform = new grade_export_form();
$mform = new grade_export_form(null, array('publishing' => $CFG->enablepublishing));

// process post information
if ($data = $mform->get_data()) {
$itemids = array();
if ($data->itemids) {
foreach ($data->itemids as $itemid=>$selected) {
if ($selected) {
$itemids[] = $itemid;
}
}
$itemidsurl = implode(",", $itemids);
} else {
//error?
$itemidsurl = '';
}

$export = new grade_export($id, $itemids, $data->export_letters);
// print the grades on screen for feedbacks
$export = new grade_export($id, $itemids, $data);
$export->display_grades($feedback, $data->previewrows);

// this redirect should trigger a download prompt
redirect('export.php?id='.$id.'&amp;itemids='.$itemidsurl.'&amp;export_letters='.$data->export_letters);
exit;
$export->print_continue('xls');
}

$mform->display();
Expand Down
37 changes: 3 additions & 34 deletions grade/export/xml/index.php
Expand Up @@ -48,45 +48,14 @@
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
print_grade_plugin_selector($id, 'export', 'xml');

$mform = new grade_export_form(null, array('idnumberrequired'=>true, 'publishing'=>true));
$mform = new grade_export_form(null, array('idnumberrequired'=>true, 'publishing'=>$CFG->enablepublishing));

// process post information
if ($data = $mform->get_data()) {
$itemids = array();
if ($data->itemids) {
foreach ($data->itemids as $itemid=>$selected) {
if ($selected) {
$itemids[] = $itemid;
}
}
$itemidsurl = implode(",", $itemids);
} else {
//error?
$itemidsurl = '';
}

// print the grades on screen for feedbacks

$export = new grade_export($id, $itemids, $data->export_letters, !empty($data->key));

$export = new grade_export($id, $itemids, $data);
$export->display_grades($feedback, $data->previewrows);

// this redirect should trigger a download prompt
if (empty($data->key)) {
print_continue('export.php?id='.$id.'&amp;itemids='.$itemidsurl.'&amp;export_letters='.$data->export_letters);

} else {
if ($data->key == 1) {
$data->key = create_user_key('grade/export', $USER->id, $COURSE->id, $data->iprestriction, $data->validuntil);
}
$link = $CFG->wwwroot.'/grade/export/xml/dump.php?id='.$id.'&amp;itemids='.$itemidsurl.'&amp;export_letters='.$data->export_letters.'&amp;key='.$data->key;

echo '<p>';
echo '<a href="'.$link.'">'.$link.'</a>';
echo '</p>';
print_footer();
}
exit;
$export->print_continue('xml');
}

$mform->display();
Expand Down

0 comments on commit 0e2d708

Please sign in to comment.