Skip to content

Commit

Permalink
Merge branch 'MDL-46548-master' of git://github.com/damyon/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Aug 5, 2014
2 parents f851224 + 9871a43 commit a43be9d
Show file tree
Hide file tree
Showing 18 changed files with 315 additions and 166 deletions.
100 changes: 63 additions & 37 deletions grade/export/grade_export_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,61 @@ class grade_export_form extends moodleform {
function definition() {
global $CFG, $COURSE, $USER, $DB;

$isdeprecatedui = false;

$mform =& $this->_form;
if (isset($this->_customdata)) { // hardcoding plugin names here is hacky
$features = $this->_customdata;
} else {
$features = array();
}

$mform->addElement('header', 'options', get_string('options', 'grades'));
if (empty($features['simpleui'])) {
debugging('Grade export plugin needs updating to support one step exports.', DEBUG_DEVELOPER);
}

$mform->addElement('header', 'gradeitems', get_string('gradeitemsinc', 'grades'));
$mform->setExpanded('gradeitems', true);

if (!empty($features['idnumberrequired'])) {
$mform->addElement('static', 'idnumberwarning', get_string('useridnumberwarning', 'grades'));
}

$switch = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);

// Grab the grade_seq for this course
$gseq = new grade_seq($COURSE->id, $switch);

if ($grade_items = $gseq->items) {
$needs_multiselect = false;
$canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($COURSE->id));

foreach ($grade_items as $grade_item) {
// Is the grade_item hidden? If so, can the user see hidden grade_items?
if ($grade_item->is_hidden() && !$canviewhidden) {
continue;
}

if (!empty($features['idnumberrequired']) and empty($grade_item->idnumber)) {
$mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), get_string('noidnumber', 'grades'));
$mform->hardFreeze('itemids['.$grade_item->id.']');
} else {
$mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), null, array('group' => 1));
$mform->setDefault('itemids['.$grade_item->id.']', 1);
$needs_multiselect = true;
}
}

if ($needs_multiselect) {
$this->add_checkbox_controller(1, null, null, 1); // 1st argument is group name, 2nd is link text, 3rd is attributes and 4th is original value
}
}


$mform->addElement('header', 'options', get_string('exportformatoptions', 'grades'));
if (!empty($features['simpleui'])) {
$mform->setExpanded('options', false);
}

$mform->addElement('advcheckbox', 'export_feedback', get_string('exportfeedback', 'grades'));
$mform->setDefault('export_feedback', 0);
Expand All @@ -48,8 +95,12 @@ function definition() {
$mform->setConstant('export_onlyactive', 1);
}

$options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
$mform->addElement('select', 'previewrows', get_string('previewrows', 'grades'), $options);
if (empty($features['simpleui'])) {
$options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
$mform->addElement('select', 'previewrows', get_string('previewrows', 'grades'), $options);
}



if (!empty($features['updategradesonly'])) {
$mform->addElement('advcheckbox', 'updatedgradesonly', get_string('updatedgradesonly', 'grades'));
Expand Down Expand Up @@ -93,7 +144,10 @@ function definition() {
}

if (!empty($CFG->gradepublishing) and !empty($features['publishing'])) {
$mform->addElement('header', 'publishing', get_string('publishing', 'grades'));
$mform->addElement('header', 'publishing', get_string('publishingoptions', 'grades'));
if (!empty($features['simpleui'])) {
$mform->setExpanded('publishing', false);
}
$options = array(get_string('nopublish', 'grades'), get_string('createnewkey', 'userkey'));
$keys = $DB->get_records_select('user_private_key', "script='grade/export' AND instance=? AND userid=?",
array($COURSE->id, $USER->id));
Expand Down Expand Up @@ -121,42 +175,14 @@ function definition() {
$mform->disabledIf('validuntil', 'key', 'noteq', 1);
}

$mform->addElement('header', 'gradeitems', get_string('gradeitemsinc', 'grades'));

$switch = grade_get_setting($COURSE->id, 'aggregationposition', $CFG->grade_aggregationposition);

// Grab the grade_seq for this course
$gseq = new grade_seq($COURSE->id, $switch);

if ($grade_items = $gseq->items) {
$needs_multiselect = false;
$canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($COURSE->id));

foreach ($grade_items as $grade_item) {
// Is the grade_item hidden? If so, can the user see hidden grade_items?
if ($grade_item->is_hidden() && !$canviewhidden) {
continue;
}

if (!empty($features['idnumberrequired']) and empty($grade_item->idnumber)) {
$mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), get_string('noidnumber', 'grades'));
$mform->hardFreeze('itemids['.$grade_item->id.']');
} else {
$mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), null, array('group' => 1));
$mform->setDefault('itemids['.$grade_item->id.']', 1);
$needs_multiselect = true;
}
}

if ($needs_multiselect) {
$this->add_checkbox_controller(1, null, null, 1); // 1st argument is group name, 2nd is link text, 3rd is attributes and 4th is original value
}
}

$mform->addElement('hidden', 'id', $COURSE->id);
$mform->setType('id', PARAM_INT);
$this->add_action_buttons(false, get_string('submit'));
$submitstring = get_string('download');
if (empty($features['simpleui'])) {
$submitstring = get_string('submit');
}

$this->add_action_buttons(false, $submitstring);
}
}

77 changes: 64 additions & 13 deletions grade/export/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ abstract class grade_export {
public $course; // course object
public $columns; // array of grade_items selected for export

public $previewrows; // number of rows in preview
public $export_letters; // export letters
public $export_feedback; // export feedback
public $userkey; // export using private user key
Expand All @@ -43,24 +42,61 @@ abstract class grade_export {
public $usercustomfields; // include users custom fields

/**
* Constructor should set up all the private variables ready to be pulled
* @deprecated since Moodle 2.8
* @var $previewrows Number of rows in preview.
*/
public $previewrows;

/**
* Constructor should set up all the private variables ready to be pulled.
*
* This constructor used to accept the individual parameters as separate arguments, in
* 2.8 this was simplified to just accept the data from the moodle form.
*
* @access public
* @param object $course
* @param int $groupid id of selected group, 0 means all
* @param string $itemlist comma separated list of item ids, empty means all
* @param boolean $export_feedback
* @param boolean $updatedgradesonly
* @param string $displaytype
* @param int $decimalpoints
* @param boolean $onlyactive
* @param boolean $usercustomfields include user custom field in export
* @param int $groupid
* @param stdClass|null $formdata
* @note Exporting as letters will lead to data loss if that exported set it re-imported.
*/
public function grade_export($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2, $onlyactive = false, $usercustomfields = false) {
public function __construct($course, $groupid, $formdata) {
if (func_num_args() != 3 || ($formdata != null && get_class($formdata) != "stdClass")) {
$args = func_get_args();
return call_user_func_array(array($this, "deprecated_constructor"), $args);
}
$this->course = $course;
$this->groupid = $groupid;

$this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id));

$this->process_form($formdata);
}

/**
* Old deprecated constructor.
*
* This deprecated constructor accepts the individual parameters as separate arguments, in
* 2.8 this was simplified to just accept the data from the moodle form.
*
* @deprecated since 2.8 MDL-46548. Instead call the shortened constructor which accepts the data
* directly from the grade_export_form.
*/
protected function deprecated_constructor($course,
$groupid=0,
$itemlist='',
$export_feedback=false,
$updatedgradesonly = false,
$displaytype = GRADE_DISPLAY_TYPE_REAL,
$decimalpoints = 2,
$onlyactive = false,
$usercustomfields = false) {

debugging('Many argument constructor for class "grade_export" is deprecated. Call the 3 argument version instead.', DEBUG_DEVELOPER);

$this->course = $course;
$this->groupid = $groupid;

$this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id));
//Populating the columns here is required by /grade/export/(whatever)/export.php
//however index.php, when the form is submitted, will construct the collection here
//with an empty $itemlist then reconstruct it in process_form() using $formdata
Expand Down Expand Up @@ -126,6 +162,10 @@ function process_form($formdata) {
$this->userkey = $formdata->key;
}

if (isset($formdata->decimals)) {
$this->decimalpoints = $formdata->decimals;
}

if (isset($formdata->export_letters)) {
$this->export_letters = $formdata->export_letters;
}
Expand Down Expand Up @@ -209,10 +249,13 @@ public abstract function print_grades();
/**
* Prints preview of exported grades on screen as a feedback mechanism
* @param bool $require_user_idnumber true means skip users without idnumber
* @deprecated since 2.8 MDL-46548. Previews are not useful on export.
*/
public function display_preview($require_user_idnumber=false) {
global $OUTPUT;

debugging('function grade_export::display_preview is deprecated.', DEBUG_DEVELOPER);

$userprofilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields);
$formatoptions = new stdClass();
$formatoptions->para = false;
Expand Down Expand Up @@ -327,19 +370,25 @@ public function get_export_params() {
/**
* Either prints a "Export" box, which will redirect the user to the download page,
* or prints the URL for the published data.
*
* @deprecated since 2.8 MDL-46548. Call get_export_url and set the
* action of the grade_export_form instead.
* @return void
*/
public function print_continue() {
global $CFG, $OUTPUT;

debugging('function grade_export::print_continue is deprecated.', DEBUG_DEVELOPER);
$params = $this->get_export_params();

echo $OUTPUT->heading(get_string('export', 'grades'));

echo $OUTPUT->container_start('gradeexportlink');

if (!$this->userkey) { // this button should trigger a download prompt
echo $OUTPUT->single_button(new moodle_url('/grade/export/'.$this->plugin.'/export.php', $params), get_string('download', 'admin'));
if (!$this->userkey) {
// This button should trigger a download prompt.
$url = new moodle_url('/grade/export/'.$this->plugin.'/export.php', $params);
echo $OUTPUT->single_button($url, get_string('download', 'admin'));

} else {
$paramstr = '';
Expand All @@ -354,6 +403,8 @@ public function print_continue() {
echo get_string('download', 'admin').': ' . html_writer::link($link, $link);
}
echo $OUTPUT->container_end();

return;
}
}

Expand Down
12 changes: 4 additions & 8 deletions grade/export/ods/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
require_once 'grade_export_ods.php';

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

if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('nocourseid');
}

require_login($course);
$context = context_course::instance($id);
$groupid = groups_get_course_group($course, true);

require_capability('moodle/grade:export', $context);
require_capability('gradeexport/ods:view', $context);
Expand All @@ -43,9 +37,11 @@
print_error('cannotaccessgroup', 'grades');
}
}
$mform = new grade_export_form(null, array('publishing' => true, 'simpleui' => true));
$data = $mform->get_data();

// print all the exported data here
$export = new grade_export_ods($course, $groupid, $itemids, $export_feedback, $updatedgradesonly, $displaytype, $decimalpoints, $onlyactive, true);
$export = new grade_export_ods($course, $groupid, $data);
$export->print_grades();


28 changes: 12 additions & 16 deletions grade/export/ods/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,25 @@
$CFG->gradepublishing = has_capability('gradeexport/ods:publish', $context);
}

$mform = new grade_export_form(null, array('publishing' => true));
$actionurl = new moodle_url('/grade/export/ods/export.php');
$formoptions = array(
'publishing' => true,
'simpleui' => true
);

$groupmode = groups_get_course_groupmode($course); // Groups are being used
$mform = new grade_export_form($actionurl, $formoptions);

$groupmode = groups_get_course_groupmode($course); // Groups are being used.
$currentgroup = groups_get_course_group($course, true);
if ($groupmode == SEPARATEGROUPS and !$currentgroup and !has_capability('moodle/site:accessallgroups', $context)) {

if (($groupmode == SEPARATEGROUPS) &&
(!$currentgroup) &&
(!has_capability('moodle/site:accessallgroups', $context))) {
echo $OUTPUT->heading(get_string("notingroup"));
echo $OUTPUT->footer();
die;
}

// process post information
if ($data = $mform->get_data()) {
$onlyactive = $data->export_onlyactive || !has_capability('moodle/course:viewsuspendedusers', $context);
$export = new grade_export_ods($course, $currentgroup, '', false, false, $data->display, $data->decimals, $onlyactive, true);

// print the grades on screen for feedbacks
$export->process_form($data);
$export->print_continue();
$export->display_preview();
echo $OUTPUT->footer();
exit;
}

groups_print_course_menu($course, 'index.php?id='.$id);
echo '<div class="clearer"></div>';

Expand Down
22 changes: 11 additions & 11 deletions grade/export/txt/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@
require_once 'grade_export_txt.php';

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

if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('nocourseid');
}

require_login($course);
$context = context_course::instance($id);
$groupid = groups_get_course_group($course, true);

require_capability('moodle/grade:export', $context);
require_capability('gradeexport/txt:view', $context);
Expand All @@ -45,8 +38,15 @@
}
}

// print all the exported data here
$export = new grade_export_txt($course, $groupid, $itemids, $export_feedback, $updatedgradesonly, $displaytype, $decimalpoints, $separator, $onlyactive, true);
$export->print_grades();
$params = array(
'includeseparator'=>true,
'publishing' => true,
'simpleui' => true
);
$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();

Loading

0 comments on commit a43be9d

Please sign in to comment.