first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+143
View File
@@ -0,0 +1,143 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A page for selecting outcomes for use in a course
*
* @package core_grades
* @copyright 2007 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once '../../../config.php';
require_once $CFG->dirroot.'/grade/lib.php';
require_once $CFG->libdir.'/gradelib.php';
$courseid = required_param('id', PARAM_INT);
$PAGE->set_url('/grade/edit/outcome/course.php', array('id'=>$courseid));
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
/// Make sure they can even access this course
require_login($course);
$context = context_course::instance($course->id);
require_capability('moodle/course:update', $context);
/// return tracking object
$gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'outcomes', 'courseid'=>$courseid));
// first of all fix the state of outcomes_course table
$standardoutcomes = grade_outcome::fetch_all_global();
$co_custom = grade_outcome::fetch_all_local($courseid);
$co_standard_used = array();
$co_standard_notused = array();
if ($courseused = $DB->get_records('grade_outcomes_courses', array('courseid' => $courseid), '', 'outcomeid')) {
$courseused = array_keys($courseused);
} else {
$courseused = array();
}
// fix wrong entries in outcomes_courses
foreach ($courseused as $oid) {
if (!array_key_exists($oid, $standardoutcomes) and !array_key_exists($oid, $co_custom)) {
$DB->delete_records('grade_outcomes_courses', array('outcomeid' => $oid, 'courseid' => $courseid));
}
}
// fix local custom outcomes missing in outcomes_course
foreach($co_custom as $oid=>$outcome) {
if (!in_array($oid, $courseused)) {
$courseused[$oid] = $oid;
$goc = new stdClass();
$goc->courseid = $courseid;
$goc->outcomeid = $oid;
$DB->insert_record('grade_outcomes_courses', $goc);
}
}
// now check all used standard outcomes are in outcomes_course too
$params = array($courseid);
$sql = "SELECT DISTINCT outcomeid
FROM {grade_items}
WHERE courseid=? and outcomeid IS NOT NULL";
if ($realused = $DB->get_records_sql($sql, $params)) {
$realused = array_keys($realused);
foreach ($realused as $oid) {
if (array_key_exists($oid, $standardoutcomes)) {
$co_standard_used[$oid] = $standardoutcomes[$oid];
unset($standardoutcomes[$oid]);
if (!in_array($oid, $courseused)) {
$courseused[$oid] = $oid;
$goc = new stdClass();
$goc->courseid = $courseid;
$goc->outcomeid = $oid;
$DB->insert_record('grade_outcomes_courses', $goc);
}
}
}
}
// find all unused standard course outcomes - candidates for removal
foreach ($standardoutcomes as $oid=>$outcome) {
if (in_array($oid, $courseused)) {
$co_standard_notused[$oid] = $standardoutcomes[$oid];
unset($standardoutcomes[$oid]);
}
}
/// form processing
if ($data = data_submitted() and confirm_sesskey()) {
require_capability('moodle/grade:manageoutcomes', $context);
if (!empty($data->add) && !empty($data->addoutcomes)) {
/// add all selected to course list
foreach ($data->addoutcomes as $add) {
$add = clean_param($add, PARAM_INT);
if (!array_key_exists($add, $standardoutcomes)) {
continue;
}
$goc = new stdClass();
$goc->courseid = $courseid;
$goc->outcomeid = $add;
$DB->insert_record('grade_outcomes_courses', $goc);
}
} else if (!empty($data->remove) && !empty($data->removeoutcomes)) {
/// remove all selected from course outcomes list
foreach ($data->removeoutcomes as $remove) {
$remove = clean_param($remove, PARAM_INT);
if (!array_key_exists($remove, $co_standard_notused)) {
continue;
}
$DB->delete_records('grade_outcomes_courses', array('courseid' => $courseid, 'outcomeid' => $remove));
}
}
redirect('course.php?id='.$courseid); // we must redirect to get fresh data
}
$actionbar = new \core_grades\output\course_outcomes_action_bar($context);
// Print header.
print_grade_page_head($COURSE->id, 'outcome', 'course', false, false, false,
true, null, null, null, $actionbar);
require('course_form.html');
echo $OUTPUT->footer();
+67
View File
@@ -0,0 +1,67 @@
<?php $maxlength=70; ?>
<form action="course.php" method="post">
<div>
<table class="courseoutcomes">
<tr>
<td>
<label for="removeoutcomes"><?php print_string('outcomescourse', 'grades'); ?></label>
<br />
<select id="removeoutcomes" size="20" name="removeoutcomes[]" multiple="multiple" class="form-control input-block-level">
<?php
if ($co_standard_notused) {
echo '<optgroup label="'.get_string('outcomescoursenotused', 'grades').'">';
foreach ($co_standard_notused as $outcome) {
echo '<option value="'.$outcome->id.'">'.shorten_text($outcome->get_name(), $maxlength).'</option>';
}
echo '</optgroup>';
}
if ($co_standard_used) {
echo '<optgroup label="'.get_string('outcomescourseused', 'grades').'">';
foreach ($co_standard_used as $outcome) {
echo '<option value="'.$outcome->id.'">'.shorten_text($outcome->get_name(), $maxlength).'</option>';
}
echo '</optgroup>';
}
if ($co_custom) {
echo '<optgroup label="'.get_string('outcomescoursecustom', 'grades').'">';
foreach ($co_custom as $outcome) {
echo '<option value="'.$outcome->id.'">'.shorten_text($outcome->get_name(), $maxlength).'</option>';
}
echo '</optgroup>';
}
?>
</select>
</td>
<?php
if (has_capability('moodle/grade:manageoutcomes', $context)) {
?>
<td class="pl-3 pr-3">
<div class="my-3">
<input name="add" class="btn btn-secondary" id="add" type="submit" value="<?php echo $OUTPUT->larrow() . ' ' .
get_string('add'); ?>" title="<?php print_string('add'); ?>" />
</div>
<div class="my-3">
<input name="remove" class="btn btn-secondary" id="remove" type="submit" value="<?php echo get_string('remove') .
' ' . $OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
</div>
</td>
<?php } ?>
<td>
<label for="addoutcomes"><?php print_string('outcomesstandardavailable', 'grades'); ?></label>
<br />
<select id="addoutcomes" size="20" name="addoutcomes[]" multiple="multiple" class="form-control input-block-level">
<?php
foreach ($standardoutcomes as $outcome) {
echo '<option value="'.$outcome->id.'">'.shorten_text($outcome->get_name(), $maxlength).'</option>';
}
?>
</select>
</td>
</tr>
</table>
<input name="id" type="hidden" value="<?php echo $courseid?>"/>
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
</div>
</form>
+177
View File
@@ -0,0 +1,177 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Edit page for grade outcomes.
*
* @package core_grades
* @copyright 2008 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once '../../../config.php';
require_once $CFG->dirroot.'/grade/lib.php';
require_once $CFG->dirroot.'/grade/report/lib.php';
require_once 'edit_form.php';
$courseid = optional_param('courseid', 0, PARAM_INT);
$id = optional_param('id', 0, PARAM_INT);
$url = new moodle_url('/grade/edit/outcome/edit.php');
if ($courseid !== 0) {
$url->param('courseid', $courseid);
}
if ($id !== 0) {
$url->param('id', $id);
}
$PAGE->set_url($url);
$PAGE->set_pagelayout('admin');
$systemcontext = context_system::instance();
$heading = get_string('addoutcome', 'grades');
// a bit complex access control :-O
if ($id) {
$heading = get_string('editoutcome', 'grades');
/// editing existing outcome
if (!$outcome_rec = $DB->get_record('grade_outcomes', array('id' => $id))) {
throw new \moodle_exception('invalidoutcome');
}
if ($outcome_rec->courseid) {
$outcome_rec->standard = 0;
if (!$course = $DB->get_record('course', array('id' => $outcome_rec->courseid))) {
throw new \moodle_exception('invalidcourseid');
}
require_login($course);
$context = context_course::instance($course->id);
require_capability('moodle/grade:manage', $context);
$courseid = $course->id;
} else {
if ($courseid) {
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
throw new \moodle_exception('invalidcourseid');
}
}
$outcome_rec->standard = 1;
$outcome_rec->courseid = $courseid;
require_login();
require_capability('moodle/grade:manage', $systemcontext);
$PAGE->set_context($systemcontext);
}
} else if ($courseid){
/// adding new outcome from course
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_login($course);
$context = context_course::instance($course->id);
require_capability('moodle/grade:manage', $context);
$outcome_rec = new stdClass();
$outcome_rec->standard = 0;
$outcome_rec->courseid = $courseid;
} else {
require_login();
require_capability('moodle/grade:manage', $systemcontext);
$PAGE->set_context($systemcontext);
/// adding new outcome from admin section
$outcome_rec = new stdClass();
$outcome_rec->standard = 1;
$outcome_rec->courseid = 0;
}
if (!$courseid) {
require_once $CFG->libdir.'/adminlib.php';
admin_externalpage_setup('outcomes');
$PAGE->set_primary_active_tab('siteadminnode');
} else {
navigation_node::override_active_url(new moodle_url('/grade/edit/outcome/course.php', ['id' => $courseid]));
$PAGE->navbar->add(get_string('manageoutcomes', 'grades'),
new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]));
}
// default return url
$gpr = new grade_plugin_return();
$returnurl = $gpr->get_return_url('index.php?id='.$courseid);
$editoroptions = array(
'maxfiles' => EDITOR_UNLIMITED_FILES,
'maxbytes' => $CFG->maxbytes,
'trusttext' => false,
'noclean' => true,
'context' => $systemcontext
);
if (!empty($outcome_rec->id)) {
$editoroptions['subdirs'] = file_area_contains_subdirs($systemcontext, 'grade', 'outcome', $outcome_rec->id);
$outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome_rec->id);
} else {
$editoroptions['subdirs'] = false;
$outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', null);
}
$mform = new edit_outcome_form(null, compact('gpr', 'editoroptions'));
$mform->set_data($outcome_rec);
if ($mform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $mform->get_data()) {
$outcome = new grade_outcome(array('id'=>$id));
$data->usermodified = $USER->id;
if (empty($outcome->id)) {
$data->description = $data->description_editor['text'];
grade_outcome::set_properties($outcome, $data);
if (!has_capability('moodle/grade:manage', $systemcontext)) {
$data->standard = 0;
}
$outcome->courseid = !empty($data->standard) ? null : $courseid;
if (empty($outcome->courseid)) {
$outcome->courseid = null;
}
$outcome->insert();
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome->id);
$DB->set_field($outcome->table, 'description', $data->description, array('id'=>$outcome->id));
} else {
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $id);
grade_outcome::set_properties($outcome, $data);
if (isset($data->standard)) {
$outcome->courseid = !empty($data->standard) ? null : $courseid;
} else {
unset($outcome->courseid); // keep previous
}
$outcome->update();
}
redirect($returnurl);
}
$PAGE->navbar->add($heading, $url);
print_grade_page_head($courseid ?: SITEID, 'outcome', 'edit', $heading, false, false, false);
if (!grade_scale::fetch_all_local($courseid) && !grade_scale::fetch_all_global()) {
echo $OUTPUT->confirm(get_string('noscales', 'grades'), $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$courseid, $returnurl);
echo $OUTPUT->footer();
die();
}
$mform->display();
echo $OUTPUT->footer();
+160
View File
@@ -0,0 +1,160 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Edit form for grade outcomes
*
* @package core_grades
* @copyright 2007 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once $CFG->libdir.'/formslib.php';
class edit_outcome_form extends moodleform {
public function definition() {
global $CFG, $COURSE;
$mform =& $this->_form;
// visible elements
$mform->addElement('header', 'general', get_string('outcomes', 'grades'));
$mform->addElement('text', 'fullname', get_string('outcomefullname', 'grades'), 'size="40"');
$mform->addRule('fullname', get_string('required'), 'required');
$mform->setType('fullname', PARAM_TEXT);
$mform->addElement('text', 'shortname', get_string('outcomeshortname', 'grades'), 'size="20"');
$mform->addRule('shortname', get_string('required'), 'required');
$mform->setType('shortname', PARAM_NOTAGS);
$mform->addElement('advcheckbox', 'standard', get_string('outcomestandard', 'grades'));
$mform->addHelpButton('standard', 'outcomestandard', 'grades');
$options = array();
$mform->addElement('selectwithlink', 'scaleid', get_string('scale'), $options, null,
array('link' => $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$COURSE->id, 'label' => get_string('scalescustomcreate')));
$mform->addHelpButton('scaleid', 'typescale', 'grades');
$mform->addRule('scaleid', get_string('required'), 'required');
$mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']);
// hidden params
$mform->addElement('hidden', 'id', 0);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'courseid', 0);
$mform->setType('courseid', PARAM_INT);
/// add return tracking info
$gpr = $this->_customdata['gpr'];
$gpr->add_mform_elements($mform);
//-------------------------------------------------------------------------------
// buttons
$this->add_action_buttons();
}
/// tweak the form - depending on existing data
function definition_after_data() {
global $CFG;
$mform =& $this->_form;
// first load proper scales
if ($courseid = $mform->getElementValue('courseid')) {
$options = array();
if ($scales = grade_scale::fetch_all_local($courseid)) {
$options[-1] = '--'.get_string('scalescustom');
foreach($scales as $scale) {
$options[$scale->id] = $scale->get_name();
}
}
if ($scales = grade_scale::fetch_all_global()) {
$options[-2] = '--'.get_string('scalesstandard');
foreach($scales as $scale) {
$options[$scale->id] = $scale->get_name();
}
}
$scale_el =& $mform->getElement('scaleid');
$scale_el->load($options);
} else {
$options = array();
if ($scales = grade_scale::fetch_all_global()) {
foreach($scales as $scale) {
$options[$scale->id] = $scale->get_name();
}
}
$scale_el =& $mform->getElement('scaleid');
$scale_el->load($options);
}
if ($id = $mform->getElementValue('id')) {
$outcome = grade_outcome::fetch(array('id'=>$id));
$itemcount = $outcome->get_item_uses_count();
$coursecount = $outcome->get_course_uses_count();
if ($itemcount) {
$mform->hardFreeze('scaleid');
}
if (empty($courseid)) {
$mform->hardFreeze('standard');
} else if (!has_capability('moodle/grade:manage', context_system::instance())) {
$mform->hardFreeze('standard');
} else if ($coursecount and empty($outcome->courseid)) {
$mform->hardFreeze('standard');
}
} else {
if (empty($courseid) or !has_capability('moodle/grade:manage', context_system::instance())) {
$mform->hardFreeze('standard');
}
}
}
/// perform extra validation before submission
function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($data['scaleid'] < 1) {
$errors['scaleid'] = get_string('required');
}
if (!empty($data['standard']) and $scale = grade_scale::fetch(array('id'=>$data['scaleid']))) {
if (!empty($scale->courseid)) {
//TODO: localize
$errors['scaleid'] = 'Can not use custom scale in global outcome!';
}
}
return $errors;
}
}
+126
View File
@@ -0,0 +1,126 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Exports selected outcomes in CSV format
*
* @package core_grades
* @copyright 2008 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once '../../../config.php';
require_once $CFG->dirroot.'/grade/lib.php';
require_once $CFG->libdir.'/gradelib.php';
$courseid = optional_param('id', 0, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
/// Make sure they can even access this course
if ($courseid) {
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
throw new \moodle_exception('invalidcourseid');
}
require_login($course);
$context = context_course::instance($course->id);
require_capability('moodle/grade:manage', $context);
if (empty($CFG->enableoutcomes)) {
redirect('../../index.php?id='.$courseid);
}
} else {
require_once $CFG->libdir.'/adminlib.php';
admin_externalpage_setup('outcomes');
}
require_sesskey();
header("Content-Type: text/csv; charset=utf-8");
// TODO: make the filename more useful, include a date, a specific name, something...
header('Content-Disposition: attachment; filename=outcomes.csv');
// sending header with clear names, to make 'what is what' as easy as possible to understand
$header = array('outcome_name', 'outcome_shortname', 'outcome_description', 'scale_name', 'scale_items', 'scale_description');
echo format_csv($header, ';', '"');
$outcomes = array();
if ( $courseid ) {
$outcomes = array_merge(grade_outcome::fetch_all_global(), grade_outcome::fetch_all_local($courseid));
} else {
$outcomes = grade_outcome::fetch_all_global();
}
foreach($outcomes as $outcome) {
$line = array();
$line[] = $outcome->get_name();
$line[] = $outcome->get_shortname();
$line[] = $outcome->get_description();
$scale = $outcome->load_scale();
$line[] = $scale->get_name();
$line[] = $scale->compact_items();
$line[] = $scale->get_description();
echo format_csv($line, ';', '"');
}
/**
* Formats and returns a line of data, in CSV format. This code
* is from http://au2.php.net/manual/en/function.fputcsv.php#77866
*
* @param string[] $fields data to be exported
* @param string $delimiter char to be used to separate fields
* @param string $enclosure char used to enclose strings that contains newlines, spaces, tabs or the delimiter char itself
* @returns string one line of csv data
*/
function format_csv($fields = array(), $delimiter = ';', $enclosure = '"') {
$str = '';
$escape_char = '\\';
foreach ($fields as $value) {
if (strpos($value, $delimiter) !== false ||
strpos($value, $enclosure) !== false ||
strpos($value, "\n") !== false ||
strpos($value, "\r") !== false ||
strpos($value, "\t") !== false ||
strpos($value, ' ') !== false) {
$str2 = $enclosure;
$escaped = 0;
$len = strlen($value);
for ($i=0;$i<$len;$i++) {
if ($value[$i] == $escape_char) {
$escaped = 1;
} else if (!$escaped && $value[$i] == $enclosure) {
$str2 .= $enclosure;
} else {
$escaped = 0;
}
$str2 .= $value[$i];
}
$str2 .= $enclosure;
$str .= $str2.$delimiter;
} else {
$str .= $value.$delimiter;
}
}
$str = substr($str,0,-1);
$str .= "\n";
return $str;
}
+259
View File
@@ -0,0 +1,259 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Import outcomes from a file
*
* @package core_grades
* @copyright 2008 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../../../config.php');
require_once($CFG->dirroot.'/lib/formslib.php');
require_once($CFG->dirroot.'/grade/lib.php');
require_once($CFG->libdir.'/gradelib.php');
require_once('import_outcomes_form.php');
$courseid = optional_param('courseid', 0, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$scope = optional_param('scope', 'custom', PARAM_ALPHA);
$url = new moodle_url('/grade/edit/outcome/import.php', array('courseid' => $courseid));
$PAGE->set_url($url);
$PAGE->set_pagelayout('admin');
/// Make sure they can even access this course
if ($courseid) {
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
throw new \moodle_exception('invalidcourseid');
}
require_login($course);
$context = context_course::instance($course->id);
if (empty($CFG->enableoutcomes)) {
redirect('../../index.php?id='.$courseid);
}
navigation_node::override_active_url(new moodle_url('/grade/edit/outcome/course.php', ['id' => $courseid]));
$PAGE->navbar->add(get_string('manageoutcomes', 'grades'),
new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]));
$PAGE->navbar->add(get_string('importoutcomes', 'grades'),
new moodle_url('/grade/edit/outcome/import.php', ['courseid' => $courseid]));
} else {
require_once $CFG->libdir.'/adminlib.php';
admin_externalpage_setup('outcomes');
$context = context_system::instance();
}
require_capability('moodle/grade:manageoutcomes', $context);
$upload_form = new import_outcomes_form();
if ($upload_form->is_cancelled()) {
redirect(new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]));
die;
}
print_grade_page_head($courseid, 'outcome', 'import', get_string('importoutcomes', 'grades'),
false, false, false);
if (!$upload_form->get_data()) { // Display the import form.
$upload_form->display();
echo $OUTPUT->footer();
die;
}
$imported_file = $CFG->tempdir . '/outcomeimport/importedfile_'.time().'.csv';
make_temp_directory('outcomeimport');
// copying imported file
if (!$upload_form->save_file('userfile', $imported_file, true)) {
redirect('import.php'. ($courseid ? "?courseid=$courseid" : ''), get_string('importfilemissing', 'grades'));
}
/// which scope are we importing the outcomes in?
if (isset($courseid) && ($scope == 'custom')) {
// custom scale
$local_scope = true;
} elseif (($scope == 'global') && has_capability('moodle/grade:manage', context_system::instance())) {
// global scale
$local_scope = false;
} else {
// shouldn't happen .. user might be trying to access this script without the right permissions.
redirect('index.php', get_string('importerror', 'grades'));
}
// open the file, start importing data
if ($handle = fopen($imported_file, 'r')) {
$line = 0; // will keep track of current line, to give better error messages.
$file_headers = '';
// $csv_data needs to have at least these columns, the value is the default position in the data file.
$headers = array('outcome_name' => 0, 'outcome_shortname' => 1, 'scale_name' => 3, 'scale_items' => 4);
$optional_headers = array('outcome_description'=>2, 'scale_description' => 5);
$imported_headers = array(); // will later be initialized with the values found in the file
$fatal_error = false;
$errormessage = '';
// data should be separated by a ';'. *NOT* by a comma! TODO: version 2.0
// or whenever we can depend on PHP5, set the second parameter (8192) to 0 (unlimited line length) : the database can store over 128k per line.
while ( $csv_data = fgetcsv($handle, 8192, ';', '"')) { // if the line is over 8k, it won't work...
$line++;
// be tolerant on input, as fgetcsv returns "an array comprising a single null field" on blank lines
if ($csv_data == array(null)) {
continue;
}
// on first run, grab and analyse the header
if ($file_headers == '') {
$file_headers = array_flip($csv_data); // save the header line ... TODO: use the header line to let import work with columns in arbitrary order
$error = false;
foreach($headers as $key => $value) {
// sanity check #1: make sure the file contains all the mandatory headers
if (!array_key_exists($key, $file_headers)) {
$error = true;
break;
}
}
if ($error) {
$fatal_error = true;
$errormessage = get_string('importoutcomenofile', 'grades', $line);
break;
}
foreach(array_merge($headers, $optional_headers) as $header => $position) {
// match given columns to expected columns *into* $headers
$imported_headers[$header] = $file_headers[$header];
}
continue; // we don't import headers
}
// sanity check #2: every line must have the same number of columns as there are
// headers. If not, processing stops.
if ( count($csv_data) != count($file_headers) ) {
$fatal_error = true;
$errormessage = get_string('importoutcomenofile', 'grades', $line);
break;
}
// sanity check #3: all required fields must be present on the current line.
foreach ($headers as $header => $position) {
if ($csv_data[$imported_headers[$header]] == '') {
$fatal_error = true;
$errormessage = get_string('importoutcomenofile', 'grades', $line);
break;
}
}
// MDL-17273 errors in csv are not preventing import from happening. We break from the while loop here
if ($fatal_error) {
break;
}
$params = array($csv_data[$imported_headers['outcome_shortname']]);
$wheresql = 'shortname = ? ';
if ($local_scope) {
$params[] = $courseid;
$wheresql .= ' AND courseid = ?';
} else {
$wheresql .= ' AND courseid IS NULL';
}
$outcome = $DB->get_records_select('grade_outcomes', $wheresql, $params);
if ($outcome) {
// already exists, print a message and skip.
echo $OUTPUT->notification(get_string('importskippedoutcome', 'grades',
$csv_data[$imported_headers['outcome_shortname']]), 'info', false);
continue;
}
// new outcome will be added, search for compatible existing scale...
$params = array($csv_data[$imported_headers['scale_name']], $csv_data[$imported_headers['scale_items']], $courseid);
$wheresql = 'name = ? AND scale = ? AND (courseid = ? OR courseid = 0)';
$scale = $DB->get_records_select('scale', $wheresql, $params);
if ($scale) {
// already exists in the right scope: use it.
$scale_id = key($scale);
} else {
if (!has_capability('moodle/course:managescales', $context)) {
echo $OUTPUT->notification(get_string('importskippedoutcome', 'grades',
$csv_data[$imported_headers['outcome_shortname']]), 'warning', false);
continue;
} else {
// scale doesn't exists : create it.
$scale_data = array('name' => $csv_data[$imported_headers['scale_name']],
'scale' => $csv_data[$imported_headers['scale_items']],
'description' => $csv_data[$imported_headers['scale_description']],
'userid' => $USER->id);
if ($local_scope) {
$scale_data['courseid'] = $courseid;
} else {
$scale_data['courseid'] = 0; // 'global' : scale use '0', outcomes use null
}
$scale = new grade_scale($scale_data);
$scale_id = $scale->insert();
}
}
// add outcome
$outcome_data = array('shortname' => $csv_data[$imported_headers['outcome_shortname']],
'fullname' => $csv_data[$imported_headers['outcome_name']],
'scaleid' => $scale_id,
'description' => $csv_data[$imported_headers['outcome_description']],
'usermodified' => $USER->id);
if ($local_scope) {
$outcome_data['courseid'] = $courseid;
} else {
$outcome_data['courseid'] = null; // 'global' : scale use '0', outcomes use null
}
$outcome = new grade_outcome($outcome_data);
$outcome_id = $outcome->insert();
$outcome_success_strings = new StdClass();
$outcome_success_strings->name = $outcome_data['fullname'];
$outcome_success_strings->id = $outcome_id;
echo $OUTPUT->notification(get_string('importoutcomesuccess', 'grades', $outcome_success_strings),
'success', false);
}
if ($fatal_error) {
echo $OUTPUT->notification($errormessage, 'error', false);
echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/import.php', ['courseid' => $courseid]),
get_string('back'), 'get');
} else {
echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]),
get_string('continue'), 'get');
}
} else {
echo $OUTPUT->box(get_string('importoutcomenofile', 'grades', 0));
}
// finish
fclose($handle);
// delete temp file
unlink($imported_file);
echo $OUTPUT->footer();
@@ -0,0 +1,62 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A form to allow importing outcomes from a file
*
* @package core_grades
* @copyright 2008 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->dirroot.'/lib/formslib.php');
class import_outcomes_form extends moodleform {
public function definition() {
global $PAGE, $USER;
$mform =& $this->_form;
$mform->addElement('hidden', 'action', 'upload');
$mform->setType('action', PARAM_ALPHANUMEXT);
$mform->addElement('hidden', 'courseid', $PAGE->course->id);
$mform->setType('courseid', PARAM_INT);
$scope = array();
if (($PAGE->course->id > 1) && has_capability('moodle/grade:manage', context_system::instance())) {
$mform->addElement('radio', 'scope', get_string('importcustom', 'grades'), null, 'custom');
$mform->addElement('radio', 'scope', get_string('importstandard', 'grades'), null, 'global');
$mform->setDefault('scope', 'custom');
}
$mform->addElement('filepicker', 'userfile', get_string('importoutcomes', 'grades'));
$mform->addRule('userfile', get_string('required'), 'required', null, 'server');
$mform->addHelpButton('userfile', 'importoutcomes', 'grades');
$buttonarray = [
$mform->createElement('submit', 'save', get_string('uploadthisfile')),
$mform->createElement('cancel')
];
$mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
}
}
+257
View File
@@ -0,0 +1,257 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Listing page for grade outcomes.
*
* @package core_grades
* @copyright 2008 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../../../config.php');
require_once($CFG->dirroot.'/grade/lib.php');
require_once($CFG->libdir.'/gradelib.php');
$courseid = optional_param('id', 0, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$url = new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]);
$PAGE->set_url($url);
$PAGE->set_pagelayout('admin');
/// Make sure they can even access this course
if ($courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_login($course);
$context = context_course::instance($course->id);
require_capability('moodle/grade:manageoutcomes', $context);
if (empty($CFG->enableoutcomes)) {
redirect('../../index.php?id='.$courseid);
}
// This page doesn't exist on the navigation so map it to another
navigation_node::override_active_url(new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)));
$PAGE->navbar->add(get_string('manageoutcomes', 'grades'), $url);
} else {
if (empty($CFG->enableoutcomes)) {
redirect('../../../');
}
require_once $CFG->libdir.'/adminlib.php';
admin_externalpage_setup('outcomes');
$context = context_system::instance();
$PAGE->set_primary_active_tab('siteadminnode');
}
/// return tracking object
$gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'outcome', 'courseid'=>$courseid));
$strshortname = get_string('outcomeshortname', 'grades');
$strfullname = get_string('outcomefullname', 'grades');
$strscale = get_string('scale');
$strstandardoutcome = get_string('outcomesstandard', 'grades');
$strcustomoutcomes = get_string('outcomescustom', 'grades');
$strcreatenewoutcome = get_string('outcomecreate', 'grades');
$stritems = get_string('items', 'grades');
$strcourses = get_string('courses');
$stredit = get_string('edit');
switch ($action) {
case 'delete':
if (!confirm_sesskey()) {
break;
}
$outcomeid = required_param('outcomeid', PARAM_INT);
if (!$outcome = grade_outcome::fetch(array('id'=>$outcomeid))) {
break;
}
if (empty($outcome->courseid)) {
require_capability('moodle/grade:manage', context_system::instance());
} else if ($outcome->courseid != $courseid) {
throw new \moodle_exception('invalidcourseid');
}
if (!$outcome->can_delete()) {
break;
}
$deleteconfirmed = optional_param('deleteconfirmed', 0, PARAM_BOOL);
if(!$deleteconfirmed){
$PAGE->set_title(get_string('outcomedelete', 'grades'));
$PAGE->navbar->add(get_string('outcomedelete', 'grades'));
echo $OUTPUT->header();
$confirmurl = new moodle_url('index.php', array(
'id' => $courseid, 'outcomeid' => $outcome->id,
'action'=> 'delete',
'sesskey' => sesskey(),
'deleteconfirmed'=> 1));
echo $OUTPUT->confirm(get_string('outcomeconfirmdelete', 'grades', $outcome->fullname), $confirmurl, "index.php?id={$courseid}");
echo $OUTPUT->footer();
die;
}else{
$outcome->delete();
}
break;
}
$systemcontext = context_system::instance();
$caneditsystemscales = has_capability('moodle/course:managescales', $systemcontext);
if ($courseid) {
$caneditcoursescales = has_capability('moodle/course:managescales', $context);
} else {
$caneditcoursescales = $caneditsystemscales;
}
$outcomes_tables = array();
$heading = get_string('outcomes', 'grades');
if ($courseid and $outcomes = grade_outcome::fetch_all_local($courseid)) {
$return = $OUTPUT->heading($strcustomoutcomes, 3, 'main mt-3');
$data = array();
foreach($outcomes as $outcome) {
$line = array();
$line[] = $outcome->get_name();
$line[] = $outcome->get_shortname();
$scale = $outcome->load_scale();
if (empty($scale->id)) { // hopefully never happens
$line[] = $scale->get_name();
debugging("Found a scale with no ID ({$scale->get_name()}) while outputting course outcomes", DEBUG_DEVELOPER);
} else {
if (empty($scale->courseid)) {
$caneditthisscale = $caneditsystemscales;
} else if ($scale->courseid == $courseid) {
$caneditthisscale = $caneditcoursescales;
} else {
$context = context_course::instance($scale->courseid);
$caneditthisscale = has_capability('moodle/course:managescales', $context);
}
if ($caneditthisscale) {
$line[] = grade_print_scale_link($courseid, $scale, $gpr);
} else {
$line[] = $scale->get_name();
}
}
$line[] = $outcome->get_item_uses_count();
$buttons = grade_button('edit', $courseid, $outcome);
if ($outcome->can_delete()) {
$buttons .= grade_button('delete', $courseid, $outcome);
}
$line[] = $buttons;
$data[] = $line;
}
$table = new html_table();
$table->head = array($strfullname, $strshortname, $strscale, $stritems, $stredit);
$table->size = array('30%', '20%', '20%', '20%', '10%' );
$table->align = array('left', 'left', 'left', 'center', 'center');
$table->width = '90%';
$table->data = $data;
$return .= html_writer::table($table);
$outcomes_tables[] = $return;
}
if ($outcomes = grade_outcome::fetch_all_global()) {
$return = $OUTPUT->heading($strstandardoutcome, 3, 'main mt-3');
$data = array();
foreach($outcomes as $outcome) {
$line = array();
$line[] = $outcome->get_name();
$line[] = $outcome->get_shortname();
$scale = $outcome->load_scale();
if (empty($scale->id)) { // hopefully never happens
$line[] = $scale->get_name();
debugging("Found a scale with no ID ({$scale->get_name()}) while outputting global outcomes", DEBUG_DEVELOPER);
} else {
if (empty($scale->courseid)) {
$caneditthisscale = $caneditsystemscales;
} else if ($scale->courseid == $courseid) {
$caneditthisscale = $caneditcoursescales;
} else {
$context = context_course::instance($scale->courseid);
$caneditthisscale = has_capability('moodle/course:managescales', $context);
}
if ($caneditthisscale) {
$line[] = grade_print_scale_link($courseid, $scale, $gpr);
} else {
$line[] = $scale->get_name();
}
}
$line[] = $outcome->get_course_uses_count();
$line[] = $outcome->get_item_uses_count();
$buttons = "";
if (has_capability('moodle/grade:manage', context_system::instance())) {
$buttons .= grade_button('edit', $courseid, $outcome);
}
if (has_capability('moodle/grade:manage', context_system::instance()) and $outcome->can_delete()) {
$buttons .= grade_button('delete', $courseid, $outcome);
}
$line[] = $buttons;
$data[] = $line;
}
$table = new html_table();
$table->head = array($strfullname, $strshortname, $strscale, $strcourses, $stritems, $stredit);
$table->size = array('30%', '20%', '20%', '10%', '10%', '10%');
$table->align = array('left', 'left', 'left', 'center', 'center', 'center');
$table->width = '90%';
$table->data = $data;
$return .= html_writer::table($table);
$outcomes_tables[] = $return;
}
$actionbar = new \core_grades\output\manage_outcomes_action_bar($context, !empty($outcomes_tables));
print_grade_page_head($courseid ?: SITEID, 'outcome', 'edit', $heading, false, false,
true, null, null, null, $actionbar);
// If there are existing outcomes, output the outcome tables.
if (!empty($outcomes_tables)) {
foreach ($outcomes_tables as $table) {
echo $table;
}
} else {
echo $OUTPUT->notification(get_string('noexistingoutcomes', 'grades'), 'info', false);
}
echo $OUTPUT->footer();
/**
* Local shortcut function for creating a link to a scale.
* @param int $courseid The Course ID
* @param grade_scale $scale The Scale to link to
* @param grade_plugin_return $gpr An object used to identify the page we just came from
* @return string html
*/
function grade_print_scale_link($courseid, $scale, $gpr) {
global $CFG, $OUTPUT;
$url = new moodle_url('/grade/edit/scale/edit.php', array('courseid' => $courseid, 'id' => $scale->id));
$url = $gpr->add_url_params($url);
return html_writer::link($url, $scale->get_name());
}
+45
View File
@@ -0,0 +1,45 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Prints navigation tabs for viewing and editing grade outcomes
*
* @package core_grades
* @copyright 2009 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$row = $tabs = array();
$context = context_course::instance($courseid);
$row[] = new tabobject('courseoutcomes',
$CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$courseid,
get_string('outcomescourse', 'grades'));
if (has_capability('moodle/grade:manage', $context)) {
$row[] = new tabobject('outcomes',
$CFG->wwwroot.'/grade/edit/outcome/index.php?id='.$courseid,
get_string('editoutcomes', 'grades'));
}
$tabs[] = $row;
echo '<div class="outcomedisplay">';
print_tabs($tabs, $currenttab);
echo '</div>';