I added a little more functionality, so my customer can choose a course to order the items.
There are 3 files involved, each is detailed.
1 - index.php - The index.php file automatically loads/executes when browsing to the containing folder. The index file does the work of
- including the other class files,
- instantiating the class files
- calling the functions in the objects to do the actual work of
- getting and sorting and updating the DB for the items in the course.
Note the very liberal use of the var_dump() function, a developers best friend!
/*this is the main class and will carry out the orders to build and execute the sorting
of the gradebook.
*/
require('../../config.php');
require('SorterDB.php');
require('SorterForm.php');
$sorterDBObj = new SorterDB;
$sorterForm = new SorterForm;
greetings();
//$sorterDBObj->greetings();
$courses = $sorterDBObj->getCourses();
//var_dump($courses);
$gui = $sorterForm->buildForm($courses);
//echo $_SERVER['REQUEST_METHOD'];
echo $gui;
if($_SERVER['REQUEST_METHOD'] == 'POST') {
//var_dump($_REQUEST);
$cid = $_REQUEST['course'];
//var_dump($cid);
$cname = $sorterDBObj->getCourseName($cid);
//var_dump($cname);
echo "
The course selected for sorting is: " .$cname->fullname."";
//get the gradeable items
$gradeableitems = $sorterDBObj->getGradeBookItems($cid);
echo " Successfully obtained gradeable items for this course
";
//var_dump($gradeableitems);
//pass gradeableitems to function to sort them
$sorterDBObj->sortGradebookItems($gradeableitems);
echo "Done sorting and updating
";
echo " Go check the gradebook items for this course, they will match the order of the course page.
";
}
function greetings(){
echo "Choose a course from the dropdown menu to sort its gradeable items.";
echo "The gradebook items will match the order of the items on the course page.";
}
This is the SorterDB class
class SorterDB{public function greetings(){
echo "
Greetings from Sorter object
";}
public function getCourses(){
global $DB;
return
$DB->get_records_sql('
SELECT id, fullname
FROM
mdl_course
ORDER BY fullname
');
}
public function getCourseName($id){
global $DB;
return
$DB->get_record_sql('
SELECT id, fullname
FROM mdl_course
WHERE id = '.$id.'
');
}
public function getGradeBookItems($courseid){
global $DB;
//$courseid = 220;
//var_dump($courseid);
return
$DB->get_records_sql('
SELECT sortorder, id, itemname, itemtype, itemmodule, iteminstance
FROM mdl_grade_items
WHERE courseid = '.$courseid.' and itemtype = "mod"
# the ORDER BY value may change if the items are acting a little wonkey~, can try itemname...
ORDER BY id
');
}
public function sortGradebookItems($items){
global $DB;
$counter = 1;
foreach($items as $row){
//echo $row->id . ' '.$counter. '
';
$recObj = new stdClass();
$recObj->id = $row->id;
$recObj->sortorder = $counter;
$DB->update_record('grade_items',$recObj,$bulk=true);
$counter++;
}
}
}
The third file SorterForm.php builds the simple form
class SorterForm{
public function __construct(){
//echo "SorterForm object has been instantiated";
}
public function buildForm($courses){
//var_dump($courses);
echo "form method="post" ";
echo "select name="course"";
foreach($courses as $row){
echo 'option value=".$row->id."'.$row->fullname.'/option';
}
echo "/select";
echo "input name="submit" type="submit"
";
echo " ";
}
}
Keep in mind that the starting php tags are in place, they are just ignored by this blogging tool. Also, my simple html code had to be removed from the SorterForm code because the blogger was trying to interpret it.
Browsing to the folder containing the 3 files results in the following:
Choosing a course from the selector results in a little conformation information displaying.


No comments:
Post a Comment